Page 1 of 1

Multiple queries help please

Posted: Wed May 07, 2003 11:43 am
by ChrisF
Hey everyone,

I'm new to PHP and have been reading a lot about it. I'm stuck however on this little output problem and was hoping someone could point me in the right direction.

I am doing a tutorial site (not for PHP obviously) and want to make an output similar to the one on http://www.htmlcenter.com/tutorials/index.cfm/main/ . I need the category name from one table to be put at the top, with a few tutorial titles from a different table underneath just like they did. Again, it will be in table format which is what confuses me. Can anybody tell me how to go about doing this? I think I could do it with about 30 queries, but there has to be a much easier and better way. Any suggestions would be greatly appreciated.

Thanks in advance,
Chris

Posted: Wed May 07, 2003 12:14 pm
by Crashin
Actually, unless I'm thinking about this too quickly, you can pull this off with two queries. The first would get all of your "Categories." Then, you would loop through each category and query to get the "Tutorial Titles" for each respective title associated with the category. For example:

Code: Select all

$query_categories = "SELECT * FROM categories";
$result_categories = mysql_query($query_categories) or die('Cannot get categories!');
while($row = mysql_fetch_array($result_categories)) {
     echo $row&#1111;'category_name'] . "<br>";
     $query_titles = "SELECT * FROM titles WHERE title=" . $row&#1111;'category_id'] or die('Cannot get titles!');
     $result_titles = mysql_query($query_titles);
     while($row_titles = mysql_fetch_array($result_titles)) &#123;
          echo $row_titles&#1111;'title_name'] . "<br>";
     &#125;
&#125;
This isn't formatted just like the example you submitted, but it'll get the job done. You could tweek it however you wanted. :)

One question about that though

Posted: Wed May 07, 2003 2:46 pm
by ChrisF
That sounds good, although I want it to be in tables... probably 3 columns and then however many rows it takes to make it... I don't see how it will work within tables though... could you explain that part?

Posted: Wed May 07, 2003 3:49 pm
by Crashin
You could try:

Code: Select all

<table width="100%" border="0" cellspacing="0" cellpadding="0">
$query_categories = "SELECT * FROM categories";
$result_categories = mysql_query($query_categories) or die('Cannot get categories!');
$i = 3;
while($row = mysql_fetch_array($result_categories)) &#123;
     if(($i % 3) == 0) &#123;?>
          <tr>
               <td width="33%"><?php
     &#125;
     else &#123;?>
               <td width="33%"><?php
     &#125;
     echo $row&#1111;'category_name'] . "<br>";
     $query_titles = "SELECT * FROM titles WHERE title=" . $row&#1111;'category_id'] or die('Cannot get titles!');
     $result_titles = mysql_query($query_titles);
     while($row_titles = mysql_fetch_array($result_titles)) &#123;
          echo $row_titles&#1111;'title_name'] . "<br>";
     &#125;
     if(($i % 3) == 0) &#123;?>
               </td><?php
     &#125;
     elseif(($i % 3) == (1 % 3)) &#123;?>
               </td><?php
     &#125;
     else &#123;?>
               </td>
          </tr><?php
     &#125;
     $i++;
&#125;
</table>
Notice that, basically, the above is looking at the $i value and dividing it by 3. If there is no remainder a new row is created, the data is displayed, and the cell is closed. If the remainder is equvalent to that of 1/3 a new cell is created, the data displayed, and the cell is simply closed. In all other cases a new cell is created, the data displayed, the cell is closed and the row is completed.

I tried and couldn't get it to work :(

Posted: Mon May 12, 2003 4:28 pm
by ChrisF
I cut and pasted what you had and then put in a <?php after the top line of your code. However, I continuously get an error, "Parse error: parse error, unexpected T_STRING in /home/understa/public_html/tutorials.php on line 47"

I'm not sure at all what that means, and cannot figure out how to fix it. If you could help with that, I would greatly appreciate it.

Posted: Tue May 13, 2003 1:26 am
by Czar
First, check in which line the error points and post it here with vars concerning it.