Answer the question
In order to leave comments, you need to log in
PHP how to display an object by category?
Good afternoon!
Strongly do not throw tomatoes, I'm not an expert in php, that's why I'm asking you.
In general, there is an object $media_this_archive of type:
array(10) {
[0]=>
object(stdClass)#203 (5) {
["id"]=>
string(2) "98"
["name"]=>
string(115) "Кафедральный собор во имя Казанской иконы Божией Матери в Чите"
["src"]=>
string(116) ""
["category_id"]=>
string(1) "0"
["tip"]=>
string(1) "1"
}
[1]=>
object(stdClass)#204 (5) {
["id"]=>
string(2) "96"
["name"]=>
string(49) "Дворец Бутиных (ГТРК "Чита")"
["src"]=>
string(116) ""
["category_id"]=>
string(1) "0"
["tip"]=>
string(1) "1"
}
[2]=>
object(stdClass)#205 (5) {
["id"]=>
string(2) "95"
["name"]=>
string(78) "Алханайский национальный парк (ГТРК "Чита")"
["src"]=>
string(116) ""
["category_id"]=>
string(1) "0"
["tip"]=>
string(1) "1"
}
[3]=>
object(stdClass)#206 (5) {
["id"]=>
string(2) "94"
["name"]=>
string(67) "Заповедник Сохондинский (ГТРК "Чита")"
["src"]=>
string(116) ""
["category_id"]=>
string(2) "37"
["tip"]=>
string(1) "1"
}
<ul class="uk-subnav uk-subnav-pill" data-uk-switcher="{connect:'#switcher-content'}">
<?php foreach ($arrayCategory as $item):?>
<li><a href="#"><?php echo getCategoryNameForId($item,$category_photo); ?></a></li>
<?php endforeach?>
</ul>
<div id="switcher-content" class="uk-switcher">
<?php foreach ($arrayCategory as $items):
$category_items =getArrayForCategoryId($item, $media_this_archive);
?>
<div class="uk-active album">
<?php foreach($category_items as $item): ?>
<?php if ($item->tip == 2):?>
<div class="items-image">
<a href="http://zabaikalom.ru/upload/sv/<?php echo $item->src; ?>.jpg" rel="lightbox[maxigallery]" target="_blank" title="<?php echo $item->name; ?>">
<img src="http://zabaikalom.ru/upload/sv/<?php echo $item->src; ?>.jpg" width="250" height="200" alt="<?php echo $item->name; ?>" title="Кликните для увеличения" class="photo"><br>
<strong><?php echo $item->name; ?></strong>
</a>
<button value="http://zabaikalom.ru/upload/sv/<?php echo $item->src; ?>.jpg" type="button" onclick="download(); return false;" class="uk-button uk-button-mini uk-button-success"> <i class="uk-icon-arrow-down"></i> Скачать</button>
</div>
<?php endif ?>
<?php if ($item->tip==1):?>
<div class="items-image">
<a class="" href="#">
<?php echo $item->src ?>
<strong><?php echo $item->name; ?></strong>
</a>
</div>
<?php endif ?>
<?php if ($item->tip==0):?>
<div><a class="items-image" href="#"><?php echo $item->src ?><strong><?php echo $item->name; ?></strong></a></div>
<?php endif ?>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
<pre>
<?php var_dump($media_this_archive) ?>
<?php var_dump($arrayCategory) ?>
</pre>
</div>
//массив категорий
$arrayCategory = getArrayCategoryForThisArchive($media_this_archive);
// получаем его так
//Получить все категории в заданном массиве
function getArrayCategoryForThisArchive($media_this_archive){
$i=0;
foreach ($media_this_archive as $item){
$array[$i++] = $item->category_id;
}
return array_unique($array);//выводим только уникальные значения
}
//Получить все из архива по ID категории
function getArrayForCategoryId($category_id, $media_this_archive){
$i=0;
foreach ($media_this_archive as $item){
if ($item->category_id === $category_id){
$array[$i++]=$item;
}
else $array = 'Что то не так';
}
return $array;
}
Answer the question
In order to leave comments, you need to log in
Shouldn't the entire list be displayed immediately in such cases, but sorted using "id="Logo", "id="web" (jQuery)?
The error says that you are trying to use an object as a string, and you cannot do this with stdClass objects.
The error contains the line where it occurs. But neither here nor on the site is it visible in which line. Look at what is there, and start from the received data.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question