0

I am new to PHP. With Laravel this is simple:

    @isset($tags)
         @foreach($tags as $tag)
             <li class="dropdown">
                 <a href="#" class="dropdown-toggle" data-toggle="dropdown"  role="button" aria-expanded="false" aria-haspopup="true">
                    {{ $tag->name }}
                </a>
                <ul class="dropdown-menu">
                    @foreach($tag->categories as $category)
                        <li><a href="{{-- route('register') --}}" {{$category->name }}</a></li>
                        <li class="divider"></li>
                    @endforeach
                 </ul>
             </li>  
        @endforeach
    @endisset

I need to use plain PHP as Laravel seems to slow considerably and I did this

<?php if(isset($tags)): ?>
    <?php foreach($tags as $tag): ?>
            <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
        <?php echo $tag['name'] ?>
    </a>
        </li>   
    <?php endforeach ?>

Now I am stuck.

The $tags display quite alright but I don't seem to get $tag->categories to dropdown.

this how my query looks like:

SELECT t.id, t.name as tag_name, c.id, c.name as c_name from tags t INNER JOIN category_tag ct ON (t.id = ct.tag_id)'.
                    'INNER JOIN categories c ON (ct.category_id = c.id) ORDER BY t.id

Any help would be nice.

4
  • Show how you fetch the data and send it to the view Commented Feb 12, 2018 at 20:18
  • I need to used plain php as laravel seem to slow considerably and I did this..... what? you know laravel is the same as php, just that renders differents tags as well? Commented Feb 12, 2018 at 20:19
  • on my shared host it is very slow! just answer to my question no more debate Commented Feb 12, 2018 at 20:26
  • there is no need to be rude dude... Commented Feb 12, 2018 at 20:29

2 Answers 2

1

Try this:

<?php 
if(isset($tags)){
     foreach($tags as $tag){ ?>
         <li class="dropdown">
             <a href="#" class="dropdown-toggle" data-toggle="dropdown"  role="button" aria-expanded="false" aria-haspopup="true">
                <?php echo $tag->name; ?> 
            </a>
            <ul class="dropdown-menu">
                <?php
                foreach($tag->categories as $category){ ?>
                    <li><a href="{{route('register')}}" <?php echo $category->name; ?> </a></li>
                    <li class="divider"></li>
                <?php } ?>
             </ul>
         </li>  
     <?php }
} 
?>

Although, i would recommend using laravel syntax. it wont make your page slower because the are just helpers that are translated to something like the above example.

Sign up to request clarification or add additional context in comments.

Comments

0

No dropdown seems normal, cause you don't have <ul class="dropdown-menu"> in your php script, like the one mentioned in your laravel blade:

<ul class="dropdown-menu">
     @foreach($tag->categories as $category)
         <li><a href="{{-- route('register') --}}" {{$category->name }}</a></li>
         <li class="divider"></li>
     @endforeach
</ul>

However, as you said, in laravel it is simple, cause of Eloquent Model relationships.

Of course, you can do it, in vanilla php in many different ways.

These 2 approaches came up my mind...

  1. Fetch categories in second query and align them to your $tag in foreach.
  2. Rebuild your query and add sub queries, to add concatenated column, which can be parsed by php.

Please note that, the first one uses more php processing, whilest the second one, causes much more load on database.

Let's focus on the second one, with mixed database processing and php...

So lets modify your query to:

SELECT t.id, t.name as tag_name,
(SELECT group_concat(
(SELECT concat_ws(':',c.id,c.name) FROM categories c WHERE ct.category_id = c.id)
) FROM category_tag ct WHERE ct.tag_id = t.id) as categories_ctn
from tags t ORDER BY t.id

Now you will have a column categories_ctnwith concatenated data separated by , with category_id:category_name

So the last thing to do, is to explode your data to arrays and modify your view script. Please note comments in this php script...

<?php if(isset($tags)): ?>
<?php foreach($tags as $tag) { ?>
        <li class="dropdown">
                <a href="#" class="dropdown-toggle" data toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
                     <?php echo $tag['tag_name'] ?>
                </a>
                <ul class="dropdown-menu">
                <?php 
                     $categories = explode(',',$tag['categories_ctn']);
                     foreach($categories as $category_data) {
                         $category = explode(':',$category_data);
                ?>

                    <li><a href="#<?php echo $category[0]; // here you get category id ?>" <?php echo $category[1]; // here you get category name ?></a></li>
                    <li class="divider"></li>
                <?php } ?>
               </ul>
       </li>   
<?php } ?>

I hope that now you have a grasp, of what you can receive from mysql query, and what you can do with that data in php.

I have not tested the code, but it should work straight away.

Thoughtful thought, do you use Laravel or not?

Cause in Laravel all mentioned above is pointless, and you can achieve the same result really simple, via Model relationships, like in the blade you mentioned...

The code mentioned above is portable, meaning you can use it in any php project, and I believe this is what your question was...

Second thought...

Remember that joining categories in mysql, will bring you one of categories instead of all...

This example is about showing all categories for tag in your menu...

And finally don't avoid Laravel, cause it is beauty one of a kind... :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.