Web Design Forum: 2 PHP questions... - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 PHP questions... Rate Topic: -----

#1 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 20 January 2012 - 08:42 PM

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!

This post has been edited by Lee_K: 20 January 2012 - 08:45 PM

0

#2 User is online   Renaissance-Design 

  • Available for custom WordPress work
  • View blog
  • Group: Moderators
  • Posts: 3,595
  • Joined: 12-August 10
  • Reputation: 559
  • Gender:Male
  • Location:South Wales
  • Experience:Web Guru
  • Area of Expertise:Designer/Coder

Posted 20 January 2012 - 09:13 PM

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

This post has been edited by Renaissance-Design: 20 January 2012 - 09:16 PM

3

#3 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 20 January 2012 - 09:48 PM

Wow! Your a star man! Thanks! I've been trying to do this all week! Amazing!
0

#4 User is offline   GarethH 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 27
  • Joined: 03-August 11
  • Reputation: 0
  • Gender:Male
  • Location:Hertfordshire
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 20 January 2012 - 11:07 PM

View PostRenaissance-Design, on 20 January 2012 - 09:13 PM, said:

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.
0

#5 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 22 January 2012 - 10:30 PM

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

This post has been edited by Lee_K: 22 January 2012 - 11:55 PM

0

#6 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 24 January 2012 - 01:25 AM

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?

This post has been edited by Lee_K: 24 January 2012 - 01:27 AM

0

#7 User is online   Renaissance-Design 

  • Available for custom WordPress work
  • View blog
  • Group: Moderators
  • Posts: 3,595
  • Joined: 12-August 10
  • Reputation: 559
  • Gender:Male
  • Location:South Wales
  • Experience:Web Guru
  • Area of Expertise:Designer/Coder

Posted 24 January 2012 - 02:00 AM

Can you post details of your tables?
1

#8 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 24 January 2012 - 07:46 PM

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!
0

#9 User is online   Gibson 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,313
  • Joined: 31-October 07
  • Reputation: 140
  • Gender:Male
  • Location:SE UK
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 24 January 2012 - 08:18 PM

View PostRenaissance%26%23045%3BDesign, on 20 January 2012 - 09:13 PM, said:

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.
0

#10 User is online   Renaissance-Design 

  • Available for custom WordPress work
  • View blog
  • Group: Moderators
  • Posts: 3,595
  • Joined: 12-August 10
  • Reputation: 559
  • Gender:Male
  • Location:South Wales
  • Experience:Web Guru
  • Area of Expertise:Designer/Coder

Posted 24 January 2012 - 08:59 PM

View PostGibson, on 24 January 2012 - 08:18 PM, said:

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.

This post has been edited by Renaissance-Design: 24 January 2012 - 09:00 PM

0

#11 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 24 January 2012 - 11:42 PM

Although i'm pleased with my progress, it seems I have a lot to learn!
0

#12 User is online   Gibson 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,313
  • Joined: 31-October 07
  • Reputation: 140
  • Gender:Male
  • Location:SE UK
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 25 January 2012 - 02:07 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users