Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

2 PHP questions...

Featured Replies

Well i'm back again! I got everything working how I wanted it to from my last thread 'PHP Tables'. Big thanks to the guys who helped me out with that!

 

Anyways, i've got 2 questions. I want to add alternate table rows into my table. At the moment i've styled them with CSS <tr class='LightPink'> and <tr class='DarkPink'>. Is it possible to use those 2 as alternate's?

 

Also, as you can see below I have a header row in my table. How can I have it so that the header row will only be displayed if a result can be found in the table?

 

Here's the code:

 

<?php

echo "<table>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Size</th>";
echo "<th>Price</th>";
echo "<th>Delivery</th>";
echo "<th></th>";
echo "</tr>";

$sql = "
(SELECT * FROM `Table1` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table2` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table3` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table4` WHERE `product_name` LIKE '%XYZ%')
ORDER BY `price` ASC";

$result = mysql_query($sql);
$nums = mysql_num_rows($result);
if($nums > 0) //Check if there is a row (over 0 means there is)
{

while($row = mysql_fetch_array($result))
{
echo "<tr class='LightPink'>";
echo "<td>".$row['program_name']."</td>";
echo "<td>30ml</td>";
echo "<td>".$row['price']."</td>";
echo "<td>Delivery</td>";
echo "<td><a href=".$row['deeplink']."rel='nofollow'>Buy</a></td>";
echo "</tr>";
}

}
else {
echo "";
}

echo "</table>";
echo "</br>";
echo "</br>";

?>

 

Thanks!

Edited by Lee_K

Couple of points:

 

Don't use PHP to echo out HTML when you can just write your HTML in the file by escaping from PHP. It's more efficient and neater to read, and it preserves indenting in the output. Secondly, when dealing with HTML in PHP templates, use the alternative conditional syntax like below, because again it's neater to read. Your classes on the table were non-semantic - you might want them to be light pink and dark pink now, but what if the design changed in the future? You'd have to edit all your templates instead of just changing the CSS, because then .lightPink would be green and .darkPink would be blue - I've renamed them to .odd and .even, which will never change.

 

Edit: Finally, it's quicker for PHP to parse strings in single quotes, because it evaluates variables in double quotes. Always use single and concatenate if you have to mix strings and variables.

 

<?php
$sql = "
(SELECT * FROM `Table1` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table2` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table3` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table4` WHERE `product_name` LIKE '%XYZ%')
ORDER BY `price` ASC";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0): ?>
<table>
	<thead>
		<tr>
		<th>Name</th>
		<th>Size</th>
		<th>Price</th>
		<th>Delivery</th>
		<th></th>
		</tr>
	</thead>
	<tbody>
<?php
$count = 1;	
while($row = mysql_fetch_array($result)):
	if(is_int($count / 2)): ?>
		<tr class="even">
		<?php else: ?>
		<tr class="odd">
		<?php endif; ?>
			<td><?php echo $row['program_name']; ?></td>
			<td>30ml</td>
			<td><?php echo $row['price']; ?></td>
			<td>Delivery</td>
			<td><a href="<?php echo $row['deeplink']; ?>" rel="nofollow">Buy</a></td>
		</tr>
	<?php 
	$count++;
endwhile; ?>
	</tbody>
</table>
<?php else: ?>
<p>No results found.</p>
<?php endif; ?>

Edited by Renaissance-Design

Couple of points:

 

Don't use PHP to echo out HTML when you can just write your HTML in the file by escaping from PHP. It's more efficient and neater to read, and it preserves indenting in the output. Secondly, when dealing with HTML in PHP templates, use the alternative conditional syntax like below, because again it's neater to read. Your classes on the table were non-semantic - you might want them to be light pink and dark pink now, but what if the design changed in the future? You'd have to edit all your templates instead of just changing the CSS, because then .lightPink would be green and .darkPink would be blue - I've renamed them to .odd and .even, which will never change.

 

Edit: Finally, it's quicker for PHP to parse strings in single quotes, because it evaluates variables in double quotes. Always use single and concatenate if you have to mix strings and variables.

 

<?php
$sql = "
(SELECT * FROM `Table1` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table2` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table3` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table4` WHERE `product_name` LIKE '%XYZ%')
ORDER BY `price` ASC";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0): ?>
<table>
	<thead>
		<tr>
		<th>Name</th>
		<th>Size</th>
		<th>Price</th>
		<th>Delivery</th>
		<th></th>
		</tr>
	</thead>
	<tbody>
<?php
$count = 1;	
while($row = mysql_fetch_array($result)):
	if(is_int($count / 2)): ?>
		<tr class="even">
		<?php else: ?>
		<tr class="odd">
		<?php endif; ?>
			<td><?php echo $row['program_name']; ?></td>
			<td>30ml</td>
			<td><?php echo $row['price']; ?></td>
			<td>Delivery</td>
			<td><a href="<?php echo $row['deeplink']; ?>" rel="nofollow">Buy</a></td>
		</tr>
	<?php 
	$count++;
endwhile; ?>
	</tbody>
</table>
<?php else: ?>
<p>No results found.</p>
<?php endif; ?>

 

I do exactly the same as you. However, the amount of people I see still echoing out HTML is quite a few, especially if you're building loads of classes.

  • Author

The code is working great but i've decided I want to add another table cell <td> to each row that is printed out. But it will come from another table. I'm not even sure if that can be done? Is it some sort of join that I need to look into?

 

Basically something like:

 

<?php
$sql = "
(SELECT * FROM `Table1` WHERE `product_name` LIKE '%XYZ%')
JOIN
(SELECT * FROM `TableXYZ` WHERE `program_name` = 'Table1')
UNION
(SELECT * FROM `Table2` WHERE `product_name` LIKE '%XYZ%')
JOIN
(SELECT * FROM `TableXYZ` WHERE `program_name` = 'Table2')
UNION
(SELECT * FROM `Table3` WHERE `product_name` LIKE '%XYZ%')
JOIN
(SELECT * FROM `TableXYZ` WHERE `program_name` = 'Table3')
UNION
(SELECT * FROM `Table4` WHERE `product_name` LIKE '%XYZ%')
JOIN
(SELECT * FROM `TableXYZ` WHERE `program_name` = 'Table4')
ORDER BY `price` ASC";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0): ?>
       <table>
               <thead>
                       <tr>
                       <th>Name</th>
                       <th>Size</th>
                       <th>Price</th>
                       <th>Delivery</th>
                       <th></th>
                       </tr>
               </thead>
               <tbody>
       <?php
       $count = 1;     
       while($row = mysql_fetch_array($result)):
               if(is_int($count / 2)): ?>
                       <tr class="even">
                       <?php else: ?>
                       <tr class="odd">
                       <?php endif; ?>
                               <td><?php echo $row['program_name']; ?></td>
                               <td>30ml</td>
                               <td><?php echo $row['price']; ?></td>
                               <td><?php echo $row['delivery']; ?</td> <<< THIS SHOULD BE FROM THE JOINED TABLE
                               <td><a href="<?php echo $row['deeplink']; ?>" rel="nofollow">Buy</a></td>
                       </tr>
               <?php 
               $count++;
       endwhile; ?>
               </tbody>
       </table>
<?php else: ?>
       <p>No results found.</p>
<?php endif; ?>

Edited by Lee_K

  • Author

Ok, so I sort of have the join working, but only if I remove the Union's. So basically it looks like

 

<?php
$sql = "
(SELECT * FROM `Table1` INNER JOIN `Delivery` ON `Table1`.`program_name`=`Delivery`.`program_name` WHERE `Table1`.`product_name` LIKE '%XYZ%')

BUT I HAVE TO DELETE THIS BIT:

------
UNION
(SELECT * FROM `Table2` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table3` WHERE `product_name` LIKE '%XYZ%')
UNION
(SELECT * FROM `Table4` WHERE `product_name` LIKE '%XYZ%')
------

ORDER BY `price` ASC";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0): ?>
       <table>
               <thead>
                       <tr>
                       <th>Name</th>
                       <th>Size</th>
                       <th>Price</th>
                       <th>Delivery</th>
                       <th></th>
                       </tr>
               </thead>
               <tbody>
       <?php
       $count = 1;     
       while($row = mysql_fetch_array($result)):
               if(is_int($count / 2)): ?>
                       <tr class="even">
                       <?php else: ?>
                       <tr class="odd">
                       <?php endif; ?>
                               <td><?php echo $row['program_name']; ?></td>
                               <td>30ml</td>
                               <td><?php echo $row['price']; ?></td>
                               <td><?php echo $row['delprice']; ?</td>
                               <td><a href="<?php echo $row['deeplink']; ?>" rel="nofollow">Buy</a></td>
                       </tr>
               <?php 
               $count++;
       endwhile; ?>
               </tbody>
       </table>
<?php else: ?>
       <p>No results found.</p>
<?php endif; ?>

 

How come it doesn't work with a UNION?

Edited by Lee_K

  • Author

Ah I managed to get it working, it was just an error in my code which is slightly different from above! Thanks for you time though! I think I might have another question probably by the end of the night, but we'll see! Thanks again!

Edit: Finally, it's quicker for PHP to parse strings in single quotes, because it evaluates variables in double quotes. Always use single and concatenate if you have to mix strings and variables.

 

Can you post up a quick example of right and wrong for this, renaissance? I'm having trouble tallying it up against the posted code.

 

Excellent tips as usual, +1 from me as well.

Can you post up a quick example of right and wrong for this, renaissance? I'm having trouble tallying it up against the posted code.

 

$var1 = 'foo';
echo 'Value of first var: ' . $var1;

// Outputs: Value of first var: foo

 

is marginally quicker to execute (because PHP doesn't have to check for & evaluate variables within the string) than:

 

$var1 = 'foo';
echo "Value of first var: $var1";

// Outputs: Value of first var: foo

 

The OP was using double quotes and concatenating which makes even less sense, because the only reason to use double quotes over single is the evaluation of variables within the string.

Edited by Renaissance-Design

Ah, I'm with you Renaissaince, thanks for that. Must admit I use the double quotes method quite a lot because I find it easier to read and construct, especially in quite complicated string/variable combinations. If there's a lot of processing needs to go on or speed is an issue, then looks like single quotes are the thing to go for.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.