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
Multiple queries help please
Moderator: General Moderators
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:
This isn't formatted just like the example you submitted, but it'll get the job done. You could tweek it however you wanted. 
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ї'category_name'] . "<br>";
$query_titles = "SELECT * FROM titles WHERE title=" . $rowї'category_id'] or die('Cannot get titles!');
$result_titles = mysql_query($query_titles);
while($row_titles = mysql_fetch_array($result_titles)) {
echo $row_titlesї'title_name'] . "<br>";
}
}One question about that though
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?
You could try:
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.
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)) {
if(($i % 3) == 0) {?>
<tr>
<td width="33%"><?php
}
else {?>
<td width="33%"><?php
}
echo $rowї'category_name'] . "<br>";
$query_titles = "SELECT * FROM titles WHERE title=" . $rowї'category_id'] or die('Cannot get titles!');
$result_titles = mysql_query($query_titles);
while($row_titles = mysql_fetch_array($result_titles)) {
echo $row_titlesї'title_name'] . "<br>";
}
if(($i % 3) == 0) {?>
</td><?php
}
elseif(($i % 3) == (1 % 3)) {?>
</td><?php
}
else {?>
</td>
</tr><?php
}
$i++;
}
</table>I tried and couldn't get it to work :(
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.
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.