Jump to content

while loop help!!


dubc07

Recommended Posts

Currently i have two tables on the DB

 

One which name is service and the other which is subcategory

 

I'm wanting to pull the service name and list it's subcategory's under it

 

my code is here

 

<?php
			  
			  $query  = "select  * from servicelink ";
$result = mysql_query($query) or die('Error, query failed');
//run the while loop that grabs all the news scripts
  

while($r=mysql_fetch_array($result)) 
   { 
     
  $cat=$r["type"];
  $magsset=$r["servicename"];
  
  
  $query2  = "select  * from subcategory WHERE cat=$cat";
$result2 = mysql_query($query2) or die('Error, query failed');
//run the while loop that grabs all the news scripts
  

while($s=mysql_fetch_array($result2)) 
   { 
     
  
  $magsset=$r["servicename"];
  $info=$r["subs"];
  $fuls="$info<br /> <br />";
  
  //$people = array($pics);
  //$trycount= count($people);
			  $apples='<table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td height="46">'.$magsset.'</td>
                    </tr>
                    <tr>
                      <td height="47">'.$fuls.'</td>
                    </tr>
                  </table>';
			  echo $apples;
			  }
			  }
			  ?>

 

This will pull from both but when it does it, it supplies the servicename twice Like this

 

Product1

sub1

 

Product1

sub2

 

Product1

sub3

 

I was just wondering if someone could help me make it just pull the service 1 and list all the subcats with out supplying the service on each one Like this

 

Product1

sub1

sub2

sub3

 

Thanks for all your help.

 

 

Link to comment
Share on other sites

Of course. You should just use 1 query to get all categories / subcategories using a join. Then create a multidimensional array to place all subcats under the parent category, then loop over that array to create the output. Using a query nested in a loop is not efficient.

Link to comment
Share on other sites

Try this should work but might now lol you were echoing in the wrong place

 

<?php
              
              $query  = "select  * from servicelink ";
$result = mysql_query($query) or die('Error, query failed');
//run the while loop that grabs all the news scripts
  

while($r=mysql_fetch_array($result)) 
   { 
     
     $cat=$r["type"];
     $magsset=$r["servicename"];
     echo "<table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td height="46">'.$magsset.'</td>
                    </tr>";
     
     $query2  = "select  * from subcategory WHERE cat=$cat";
$result2 = mysql_query($query2) or die('Error, query failed');
//run the while loop that grabs all the news scripts
  

while($s=mysql_fetch_array($result2)) 
   { 
     
     $info=$s["subs"];
     $fuls="$info<br /> <br />";
     
     //$people = array($pics);
     //$trycount= count($people);
              $apples='<tr>
                      <td height="47">'.$fuls.'</td>
                    </tr>
                  </table>';
              echo $apples;
              }
              }
              ?>

Link to comment
Share on other sites

Like I said this is inefficient!

You should construct an array as below using just a single query. If there are 10 categories then your code is running 10 queries plus the main query (11 queries).

Array
(
    [0] => Array
        (
            [maincat] => category 1
            [subcats] => Array
                (
                    [0] => sub cat 1
                    [1] => sub cat 2
                    [2] => sub cat 3
                )

        )

    [1] => Array
        (
            [maincat] => category 2
            [subcats] => Array
                (
                    [0] => sub cat 1
                    [1] => sub cat 2
                    [2] => sub cat 3
                )

        )

    [2] => Array
        (
            [maincat] => category 3
            [subcats] => Array
                (
                    [0] => sub cat 1
                )

        )

)


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.