trying to put code into a table

General discussions related to php

Moderators: macek, egami, gesf

beginner123
New php-forum User
New php-forum User
Posts: 33
Joined: Fri Jan 20, 2012 10:39 am

trying to put code into a table

Postby beginner123 » Fri Oct 19, 2012 10:29 am

I have created a search bar for my website to display the product information when you enter a product name in the search bar. I want the results of the search to be in a table format but I keep getting errors

Heres the code:

Code: Select all

<?php
mysql_connect ( 'localhost', 'root', "", 'k00127082')
or die (mysql_error());mysql_select_db ("k00127082");
 
$term = $_POST['term'];
$sql = mysql_query("select * from products where product_name like '%$term%' ");

while ($row = mysql_fetch_array($sql))
{   
   echo '<table>';
   echo '<tr>';
   echo '<br/> Product Name: '.$row['product_name'];   
   echo '<br/> product description: '.$row['product_description'];
   echo '<br/> product price: €'.$row['price'];   
   echo '<br/> Image: '.$row['image'];   
   echo '<br/><br/>';
   echo '</tr>';
   echo '</table>';   
}

?>

instead of the <br/> i tried putting in <td> but it doesn't work.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: trying to put code into a table

Postby johnj » Fri Oct 19, 2012 10:07 pm

The correct format is <table><tr><td></td></tr></table>

beginner123
New php-forum User
New php-forum User
Posts: 33
Joined: Fri Jan 20, 2012 10:39 am

Re: trying to put code into a table

Postby beginner123 » Sat Oct 20, 2012 5:47 am

yes i know i am having problems with the <td> tags in the code. whenever i put them in i get errors

Code: Select all

echo '<table>';
echo '<tr>';
echo '<td> Product Name: '.$row['product_name'] </td>';  // if i do this i get errors
echo '<br/> product description: '.$row['product_description'];
echo '<br/> product price: €'.$row['price'];   
echo '<br/> Image: '.$row['image'];   
echo '<br/><br/>';
echo '</tr>';
echo '</table>';   

tkdbb84
New php-forum User
New php-forum User
Posts: 137
Joined: Tue Mar 08, 2011 9:25 am

Re: trying to put code into a table

Postby tkdbb84 » Tue Oct 23, 2012 10:56 am

single quotes don't parse variables:

Code: Select all

echo '<table>';
echo '<tr>';
echo '<td>Product Name: ',$row['product_name'],'</td>';
echo '<td>Product Description: ',$row['product_description'],'</td>';
echo '<td>Product Price: €',$row['price'],'</td>';
echo '<td>Image: ',$row['image'],'</td>'; //although don't you want img tag here?!?!
echo '</tr>';
echo '</table>';


alternatively you could include a variable in a double quoted string "$myVar"; however if you are using an array, then you can't use the quotes inside the array, and thus you will generate a warning:

Code: Select all

echo "$row['product_name']"; //<--kicks an error
echo "$row[product_name]"; //<--works but generates a warning


but since echo is nice enough to take multiple arguments, you can just use commas to feed it argument after argument

hope that helps.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: trying to put code into a table

Postby seandisanti » Tue Oct 23, 2012 1:01 pm

Here's how I did something similar recently.

Code: Select all

<table>
   <tr>
      <th style="padding:10px;">E-mail</th>
      <th style="padding:10px;">First Name</th>
      <th style="padding:10px;">Last Name</th>
   </tr>
   <?php
      while ($result=mysql_fetch_assoc($results))
      {
   ?>
   <tr>
      <td style="padding:10px;"><a href="profile.php?uid=<?php echo $result['id']; ?>"><?php echo $result['email']; ?></a></td>
      <td style="padding:10px;"><?php echo $result['first_name']; ?></td>
      <td style="padding:10px;"><?php echo $result['last_name']; ?></td>
   </tr>
   <?php } ?>
</table>


just another way to skin the same cat. i personally like doing it that way, because plain HTML is more readable for me than echo'd html

tkdbb84
New php-forum User
New php-forum User
Posts: 137
Joined: Tue Mar 08, 2011 9:25 am

Re: trying to put code into a table

Postby tkdbb84 » Tue Oct 23, 2012 1:16 pm

Seadisanti, have you considered formatting your echo's as HTML, you get the best of both worlds, readability on both level.

If I format your example:

Code: Select all

        <table>
            <tr>
                <th style="padding:10px;">
                    E-mail
                </th>
                <th style="padding:10px;">
                    First Name
                </th>
                <th style="padding:10px;">
                    Last Name
                </th>
            </tr>
<?php
    while ($result=mysql_fetch_assoc($results))
    {
        echo '<tr>',
                    '<td style="padding:10px;">',
                        '<a href="profile.php?uid=',$result['id'],'">,$result['email'],'</a>',
                    '</td>',
                    '<td style="padding:10px;">',
                        $result['first_name'],
                    '</td>',
                    '<td style="padding:10px;">',
                        $result['last_name'],
                    '</td>',
                '</tr>';
    }
?>
        </table>


Then there is no need to wade through embedded <?php?> tags throughout the html. Additionally I find it annoying to wade through bracketed {} segments that aren't enclosed in <?php?> tags, as single tags can get lost in hundreds of lines of html.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: trying to put code into a table

Postby seandisanti » Tue Oct 23, 2012 2:26 pm

It's a practice i typically avoid in my examples, even though i am notorious for it around my office. I definitely understand everyone's preference for not blending html and php the way that i do it, but I personally consider it a little easier to read the way I do it, because in notepad plus (where i am typically looking at my sources) there are contextual colors and hightlighting for html and php, but echo'd html is a string, not highlighted text. it's not a big deal, but with proper indenting and syntax highlighting i think it helps the readability rather than hurting it.

***edit***
removed redundancy and attached image to demonstrate difference. With the html being output here the highlighting doesn't matter much, but when the output contains jquery functions etc. it can be a pretty big difference. above the blue line is the way that i usually do it, and below is the suggested alternative.

Just including to illustrate why i still intentionally exercise that one bad practice (and probably several i'm as yet unaware of) in my personal code.
Attachments
example.JPG
example.JPG (148.6 KiB) Viewed 525 times


Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 2 guests