Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF Statement blues! 1

Status
Not open for further replies.

EvilAsh

Technical User
Oct 22, 2005
56
GB
Hi there.

I am trying to build an IF statement that will control the value of a variable (that will be part of a url that will be added later in a script) based on the value returned in a SQL statement.

The way I thought it would work is:

First set the variables:
Code:
$v1='product';
$v2='photoproduct';

The statement being (where the value of 'gallery', returned by SQL, is either 'art' or something else):

Code:
if ($gallery == 'ART')
{
$PAGE=$v1;
}
 else
{
 $PAGE=$v2;
 }
Unfortunately, the $page variable does not change. It returns the value of $v1 regardless of the value of $gallery.

I am sure there is a minor tweak needed but my brain is tired!! Please could you offer any advice?

Many thanks.



 
The query I used in development returns two records - one where gallery=art and one where it does not.

The $page determines the destination URL within the link attched to each record so I was able to easily determine which should go to one place and which to another.

The full script is as follows - might I have placed the IF statement in the wrong place??

Code:
 <?php

$v1='product';
$v2='photoproduct';
// the guts & the output

//connect to server

$connection=mysql_connect("localhost","","")       or die("Cannot connect to mysql server");

//connect to database

$db = mysql_select_db("uniquecrauk")         or die ("Cannot connect to database");


//execute query



$query = "SELECT
*
FROM
gallery
where gallery.special = 'yes'

  ";; 

$result = mysql_query($query)    or die ("Cannot do de query");
$numrows=mysql_num_rows($result);

//url determinator
if ($gallery = 'ART')
  {
 
$PAGE=$v1;

  }
 else
 
   {
 
$PAGE=$v2;

  }

// nil returns
if ($numrows == 0)
  {
 
  echo "<font color='#000000' size='2' face='Arial, Helvetica, sans-serif'><strong>Sorry, your search returned no results. Please try again</strong></font>";


  }



echo "<table width='100%' border='0' align='left' cellspacing ='15'>";

echo "
<tr><td colspan='6'><hr color='black' size='1'></td></tr>\n";

//show results

while($i = mysql_fetch_row($result)) { 

echo"
<tr>";

//Product Thumbnail Image Link
echo "
<td ><font color='#000000' size='2' face='Arial, Helvetica, sans-serif'><A HREF=\"javascript:popUp('$PAGE.php?id=$i[0]')\"><img src=
$i[3] alt='Click for larger image.'></A></font></td>";

//Product ID
echo "
<td ><font color='#000000' size='2' face='Arial, Helvetica, sans-serif'>
Product ID: $i[0]</font></td>\n";

//Product Name
echo "
<td ><font color='#000000' size='2' face='Arial, Helvetica, sans-serif'><strong>
$i[1]</strong></font></td>\n";

//Product Price
echo "
<td ><font color='#000000' size='2' face='Arial, Helvetica, sans-serif'>
£$i[2]</font></td>\n";

</font></td>\n
</tr>
\n";
echo "<tr><td colspan='6'><hr color='black' size='1'></td></tr>";

}
echo "</table>";



?>

Thanks.
 
1) The if statement in your second posting says if ($gallery = 'ART').... in other words, was the value 'ART' properly assigned to the variable $gallery.... which will always be true. In your first example, you used the proper form, if ($gallery == 'ART')

2) $gallery is empty, once you fix the =/== issue, you're going to always get false. You need to use mysql_fetch_row() or mysql_fetch_assoc() and then access the appropriate element.
 
It's not that the if-statement is in the wrong place.

It's that at not time in this part of your script:
Code:
$query = "SELECT
*
FROM
gallery
where gallery.special = 'yes'

  ";; 

$result = mysql_query($query)    or die ("Cannot do de query");
$numrows=mysql_num_rows($result);

//url determinator
if ($gallery = 'ART')

Do you actually fetch a value from the result handle $result and put the value in $gallery before you hit the beginning of your if-statement.



Want the best answers? Ask the best questions! TANSTAAFL!
 
DOH!!!

I put the declaration in the wrong place.

The code now reads:

Code:
//show results

while($i = mysql_fetch_row($result)) { 

$gallery = $i[11];

// DECLARE URL
if ($gallery == 'ART')
{
$page=$v1;
}
 else
{
 $page=$v2;
 }

Thanks anyway.
 
Fair warning: You have precarious code... combining SELECT * & mysql_fetch_row & a specific numeric index is not recommended. A re-install of your database or a port to a new server well quite likely screw things up.

You'd be better served by explicitly stating your select critera and/or using mysql_fetch_assoc()
 
Duly noted. Thanks.

I am still on the learning curve with PHP so I am forever finding "features" like that!

I will incorporate your advice when recoding the site this weekend.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top