maxxtraxx Posted September 11, 2007 Share Posted September 11, 2007 Not sure if this is the correct forum for this but here goes: I have a products table that I want to echo the results of 3 different categories, I can query the db just fine and get results for a single category, but I'm a newb to PHP and don't know the trick way to do slick recursion. so far i've got: <?php $query1 = "SELECT group_id, name from products where group_id = 1"; $result1 = mysql_query($query1); ?> and then down in my page where i want to display the results i've got: <table width="500" border="0" cellspacing="0" cellpadding="0"> <?php while($row = mysql_fetch_array($result1)){ ?> <tr> <?php echo ('<td width="163">'); echo ("$row[name]"); echo ('</td>'); ?> </tr> </table> i know this isn't what i want/need, but its what i've got so far. what i want is to create a table with 3 columns that display the results of my products for each seperate category. thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/ Share on other sites More sharing options...
teng84 Posted September 12, 2007 Share Posted September 12, 2007 <? $query1 = "SELECT group_id, name from products where group_id = 1"; $result1 = mysql_query($query1); echo '<table> <tr> <td>field1</td> <td>field2</td> <td>field3</td> </tr>' while($row = mysql_fetch_array($result1)){ echo '<tr> <td>'.$row[name].'</td> <td>'.$etc.'</td> <td>'.$etc.'</td> </tr>'; } echo '</table>'; ?> try it that way better note: not tested Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/#findComment-346487 Share on other sites More sharing options...
liebs19 Posted September 12, 2007 Share Posted September 12, 2007 To keep doing it the way you originally had it do something like this: <table width="500" border="0" cellspacing="0" cellpadding="0"> <?php while($row = mysql_fetch_array($result1)) { ?> <tr> <td width="163"> <?=$row['group_id'];?> </td> </tr> <tr> <td width="163"> <?=$row['name'];?> </td> </tr> <tr> <td width="163"> <?=$row['whatever'];?> </td> </tr> <?php } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/#findComment-346489 Share on other sites More sharing options...
teng84 Posted September 12, 2007 Share Posted September 12, 2007 To keep doing it the way you originally had it do something like this: <table width="500" border="0" cellspacing="0" cellpadding="0"> <?php while($row = mysql_fetch_array($result1)) { ?> <tr> <td width="163"> <?=$row['group_id'];?> </td> </tr> <tr> <td width="163"> <?=$row['name'];?> </td> </tr> <tr> <td width="163"> <?=$row['whatever'];?> </td> </tr> <?php } ?> </table> this way of coding may be good but this is dirty and hard to maintain you should separate PHP from HTML Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/#findComment-346492 Share on other sites More sharing options...
liebs19 Posted September 12, 2007 Share Posted September 12, 2007 I guess it is all about personal choice. I posted it this way to show that he could continue with it the way he was in his original post. I use echo when i only need to do a few lines of code out, but when it is something like a table with multiple columns or an entire form it saves me a lot of typing to drop out of php and do the straight HTML. Both will work, just do whatever one makes sense to you. Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/#findComment-346496 Share on other sites More sharing options...
teng84 Posted September 12, 2007 Share Posted September 12, 2007 I work in a team and that kind of coding is strictly prohibited that is very dirty I use variables them echo it on my html but putting together just the way you want LOL i guess my team leader will yell at me like mad any way do what you want but this is a tip its better to do it on separate manner Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/#findComment-346499 Share on other sites More sharing options...
liebs19 Posted September 12, 2007 Share Posted September 12, 2007 Yeah, definately you need to do whatever your team has for rules. However, when coding by yourself you should do whatever is easiest for you. Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/#findComment-346500 Share on other sites More sharing options...
teng84 Posted September 12, 2007 Share Posted September 12, 2007 nah. what ever :-\ remember there is good but still there is better which you have to learn ??? i guess tru process youll understand why its better to do it separately. im not going to argue so do it your way Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/#findComment-346505 Share on other sites More sharing options...
liebs19 Posted September 12, 2007 Share Posted September 12, 2007 I understand how to do it with everything in echo. Its is just easier for me to understand for certain things by dropping out of php. Both of them get you the same results, so one is not "better" than the other. I use both while coding. I'm not trying to argue that it is better to drop out of php. I'm simply stating that it is more important that you can read your code than which method you use. Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/#findComment-346508 Share on other sites More sharing options...
maxxtraxx Posted September 12, 2007 Author Share Posted September 12, 2007 wow, awesome, thank you very much for all your help! Is there a book or site you can point me to for some "best practice" coding methods, i.e.- separating your PHP from HTML, things like that? phpfreaks, rule! Quote Link to comment https://forums.phpfreaks.com/topic/68927-solved-echoing-multiple-arrays/#findComment-346635 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.