PHP MySQL Problems with IF statement
petewalton
Status: New User - Welcome
Joined: 19 Jun 2006
Posts: 2
Reply Quote
I'm very new to MySQL/Php but over the last few weeks have managed to piece together the bits I need.

I'd now be grateful for anyone who could help me here. I'm running a MySQL query that displays results in 2 columns (items in an online shop) - this works fine. Each item has a thumbnail image (the images are stored on the server and the links to them ($Kleines_Bild contains the full path) are stored in the database) and I'm trying to include an if statement which employs file_exists to display a default image if no product image has been uploaded to the server.

It worked fine before I tried to add the parts in bold :(

<?
$Artikel_Nr=mysql_result($result,$i,"Artikel_Nr");
$Kleines_Bild=mysql_result($result,$i,"Kleines_Bild");

$division_point = ceil(@mysql_num_rows($result)/2);
$i = 0;

while ($row = mysql_fetch_array($result)) {

$item = "<table><tr><td width=\"10\"></td><td valign=\"top\">" . getimageurl($row) . "</td><td width=\"10\"></td><td width=\"350\" valign=\"top\"><b>" . stripslashes($row['Seite_Link']) . "</b><br>" . stripslashes($row['Herkunft']) . "<br>" . stripslashes($row['Verp_Inhalt']) . "<br>". stripslashes($row['VP_Brutto']) . " Eur<br>". stripslashes($row['PayPal_Taste']) . "</td></tr></table>" ;

function getimageurl($row) {
$filename = "../../b/produkte/$k/$Artikel_Nr-th.jpg";
if (file_exists($filename)) {
echo $Kleines_Bild;
} else {
echo '<img src="http://elsibarita.de/b/produkte/kein_bild.gif" \>';
}
}


if (intval($i/$division_point) > 0) {
$column_b .= "$item";
} else {
$column_a .= "$item";
}
$i++;
}

?>

<table>
<tr valign="top"><td>
<? echo $column_a; ?></td>
<td width="31"></td><td>
<? echo $column_b; ?></td>
</tr>
</table>


If it helps, here's how the results are displayed:
elsibarita.de/ud1.php?k=SO01

Using: MySQL 4.1.10a-Max

I'd be hugely grateful for any help. Thanks in advance.

Pete
Back to top
jeffd
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
:: Code ::
$Artikel_Nr=mysql_result($result,$i,"Artikel_Nr");
$Kleines_Bild=mysql_result($result,$i,"Kleines_Bild");


This has to be in the while loop, not outside of it.

Currently, you are placing a 'false' value into $i, so of course you will never get any output from these mysql results.

Once they are in the while loop, $i will have the desired numeric integer value set by the loop counter, and will extract the correct result.
Back to top
petewalton
Status: New User - Welcome
Joined: 19 Jun 2006
Posts: 2
Reply Quote
Hi jeffd, thanks for taking the time to offer a suggestion. I've played around with it and solved the problem.

For any other newbie's like me looking for a something similiar, here's my full code:

<?
include 'srv/www/path/to/file';

$query="SELECT * FROM elsibarita_produkte WHERE Kategorie_Nr='$k' AND Sichtbar='j' ORDER BY Artikel";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "";

?>




<?
$division_point = ceil(@mysql_num_rows($result)/2);
$i = 0;
$img = "";

while ($row = mysql_fetch_array($result)) {

$Artikel_Nr=mysql_result($result,$i,"Artikel_Nr");
$Kategorie_Nr=mysql_result($result,$i,"Kategorie_Nr");
$img = "../../b/produkte/$k/$Artikel_Nr-th.jpg";
if (!file_exists($img)) {
$img = '<img src="http://elsibarita.de/b/produkte/kein_bild-th.gif" \>';
} else {
$img = "<img src=\"http://elsibarita.de/b/produkte/$Kategorie_Nr/$Artikel_Nr-th.jpg\"";
}


$item = "<table><tr><td width=\"10\"></td><td valign=\"top\">" . stripslashes($img) . "</td><td width=\"10\"></td><td width=\"350\" valign=\"top\"><b>" . stripslashes($row['Seite_Link']) . "</b><br>" . stripslashes($row['Herkunft']) . "<br>" . stripslashes($row['Verp_Inhalt']) . "<br>". stripslashes($row['VP_Brutto']) . " Eur<br>". stripslashes($row['PayPal_Taste']) . "</td></tr></table>" ;

if (intval($i/$division_point) > 0) {
$column_b .= "$item";
} else {
$column_a .= "$item";
}
$i++;
}

?>


<table>
<tr valign="top"><td>
<? echo $column_a; ?></td>
<td width="31"></td><td>
<? echo $column_b; ?></td>
</tr>
</table>
Back to top
Display posts from previous:   

All times are GMT - 8 Hours