...

View Full Version : using php to create product catalog....



chump2877
02-02-2005, 12:44 AM
Using php, I'm trying to make the rows of my product catalog alternate colors, like the catalog looks on this page:

http://www.mediamogulsweb.com/accessories.shtml#product_block

So I devised a php script to perform database queries, populate my catalog, and automatically format my tables with alternating colors.....everything works except for the colors...see this page:

http://www.mediamogulsweb.com/create_component_catalog.php

Green is a good color for grass, but not for my catalog....haha

I specify two different hexadecimal shades of gray in my code, but it no worky....Here is the code:


<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF">


<p><font color="#3333CC" size="+2" face="Tunga"><strong><font color="#666666" face="Palatino Linotype"><em><font face="Times New Roman, Times, serif">Accessories</font></em></font></strong></font></p>
<table border="0" cellspacing="1" cellpadding="8" width="650">
<tr bgcolor="#666666">
<td><b><font color="#FFFFFF" face="Palatino Linotype">Item #</font></b></td>
<td><b><font color="#FFFFFF" face="Palatino Linotype">Description</font></b></td>
<td><b><font color="#FFFFFF" face="Palatino Linotype">Price</font></b></td>
</tr>


<?php

// File create_component_catalog.php


$dbcon = mysql_connect("localhost","username","password");

mysql_select_db("MyDatabase",$dbcon);


$queryA = "SELECT product_code FROM parts WHERE product_code LIKE '%AC%'";
$resultA = mysql_query($queryA);
$limit = mysql_num_rows($resultA);


$color1 = "#CCCCCC";
$color2 = "#E5E5E5";


function odd_even($num)
{
if(!is_long($num/2))
{
$rtn = 1;
}
else
{
$rtn = 0;
}
return $rtn;
}



for ($i = 1; $i <= $limit; $i++)
{
if (odd_even($i) == 0)
$row_color = $color1;
else
$row_color = $color2;

$query = "SELECT product_code FROM parts WHERE product_code LIKE '%AC%' AND RECNO = $i";
$result = mysql_query($query);
if (mysql_num_rows($result) >= '1')
{
while ($row = mysql_fetch_assoc($result))
{
echo '<tr bgcolor="$row_color">
<td>', $row['product_code'], '</td>';
}
}
else
{
echo '<span style="font-style:italic">No records found.</span><br><br>';
}

$query2 = "SELECT description FROM parts WHERE product_code LIKE '%AC%' AND RECNO = $i";
$result2 = mysql_query($query2);
if (mysql_num_rows($result2) >= '1')
{
while ($row2 = mysql_fetch_assoc($result2))
{
echo '<td>', $row2['description'], '</td>';
}
}
else
{
echo '<span style="font-style:italic">No records found.</span><br><br>';
}

$query3 = "SELECT price FROM parts WHERE product_code LIKE '%AC%' AND RECNO = $i";
$result3 = mysql_query($query3);
if (mysql_num_rows($result3) >= '1')
{
while ($row3 = mysql_fetch_assoc($result3))
{
echo '<td>$', $row3['price'], '</td></tr>';
}
}
else
{
echo '<span style="font-style:italic">No records found.</span><br><br>';
}
}



?>



</table>


</BODY>
</HTML>

Can anyone see where my problem with the color lies?...thanks in advance :)

Kurashu
02-02-2005, 01:47 AM
Two things:
First: Use $i % 2 == 0 instead of your function to check for even or odd
Second: Your background color is being set to $row_color, no its value. Use ' . $row_color . ' to use variables inside of single quotes.

However, with echo, it is faster to use commas with it, as you seem to be using.

echo '<tr bgcolor="', $row_color, '">';

Mhtml
02-02-2005, 01:54 AM
String literals.

echo '$var'; //outputs same as echo "\$var"
echo "$var";//outputs same as echo $var



First: Use $i % 2 == 0 instead of your function to check for even or odd
I believe that requires even more processing than:


if($alt){

}else{

}
$alt = !$alt;

It's limited to simple binary arithmetic there, no real calculation.

Kurashu
02-02-2005, 02:04 AM
Actually, I retract my previous statement, $i % 2 would be the fastest.

Edit: And he is checking for even and odd, not if a value is set or not, but that is is how it works. Modulus divides $a ($i for this model) by $b (2 in this model) and checks the remainder. If it $a is even, it returns zero, and thus false. But if it is odd, it will return some number and thus true.

Faster than your wonky if else statement.

chump2877
02-02-2005, 10:19 AM
Thanks for the help fixing the color of my catalog and the tip for finding even/odd numbers....Everything works now.... :thumbsup:

Mhtml
02-02-2005, 01:34 PM
Faster than your wonky if else statement.

Sigh, before you get all excited about your own supposed superiority think about the maths behind this. You are doing divisions with modulus, I am just inverting a single bit.

Well these are the times I get for both methods:


Modulus = 0.000109
Invert = 9.7E-5

Now is it me, or do I only count 3 zero's after that decimal point in the modulus time? The only thing wonky here is your maths.

[edit:] Managed to get slightly faster times for both of them on a faster machine, however modulus is still lagging. But for ranges from 0 - 10, 0 - 100 and 0 - 1000 inverting still kicks modulus butt.

firepages
02-02-2005, 03:09 PM
its even simpler than that , if/else in this case is a simple comparison , 50% of the time the }else{ is never even seen by the interpreter.

regardless , both methods are valid , the modulus is the more popular method employed though personally if/else makes more sense to me where there are only 2 possible states.

anyway , I posted to say keep it nice , any more bickering & I close the thread

Mhtml
02-03-2005, 02:09 AM
Perhaps I read wrong I did only just wake up so more than likely, both methods will need to use an if/else control structure. I was just proposing a faster condition.



if($alt){
$row_color = $color1;
}else{
$row_color = $color1;
}
$alt = !$alt;

vs


if($i%2==0){
$row_color = $color1;
}else{
$row_color = $color1;
}

I didn't mean anything by my somewhat colorful post, was pretty tired at the time it must of been like 1am.

Kurashu
02-03-2005, 02:57 AM
Either could be reduced a ternary expression as well.



//color1 for true, color2 for false
$color = ($i%2==0) ? $color1 : $color2; //remove == 0 for reverse
$color = ($alt) ? $color1 : $color2;


I now know what your if statement was doing now too. It took some thought, something I wasn't able to clearly do at the time. Stupid flu-cold-sickness thingy. Oh well

Also, on a more personal note, I thought I was spelling color wrong.

Mhtml
02-03-2005, 03:02 AM
Fair enough.
I would write it that way in my own code, but for clarity I write it as a standard control structure.

And as for color, we actually spell it colour here in Aus, but most programming and markup languages spell it color so I stick to that way on the web.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum