I
I
Igor Pushkarsky2014-07-19 13:31:48
PHP
Igor Pushkarsky, 2014-07-19 13:31:48

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"
  }

What you want to get from it is such a miracle output , that is, at the top we should see a list of categories that are read by category_id, and at the bottom, naturally, the output of the contents of each category, including the button all.
What I've done:
<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;
}

and what I did seems to work, but not always, namely with pictures everything is fine, although I may be wrong, but with frames I get an error : Object of class stdClass could not be converted to string in on line .
Maybe there is another solution to do this? Maybe another algorithm? It is advisable not to climb into the database every time and do WHERE, otherwise it slows down.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-07-19
@TsarS

Shouldn't the entire list be displayed immediately in such cases, but sorted using "id="Logo", "id="web" (jQuery)?

D
Dmitry Salodki, 2014-07-19
@dimedr

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 question

Ask a Question

731 491 924 answers to any question