Jump to content

[SOLVED] echoing multiple arrays


maxxtraxx

Recommended Posts

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!

 

 

 

Link to comment
Share on other sites

<?
$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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :D any way do what you want but this is a tip

its better to do it on separate manner 

Link to comment
Share on other sites

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.

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.