Results 1 to 5 of 5
Related
-
perl and mysql-compare 2 columns Forum: CGI Perl Forum
Replies: 7 -
need help displaying results with links Forum: PHP Forum
Replies: 0 -
PHP displaying search results Forum: PHP Forum
Replies: 24 -
Displaying Paged Results In A Table Forum: Javascript Forum
Replies: 4 -
Downgrade to non-sub query, but how to change this mysql statement?? Forum: PHP Forum
Replies: 0
-
06-17-2006, 06:18 AM #1
How to insert IF statement into MySQL query displaying results in 2 columns?
Hi,
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 are stored in the database) and I'd like to insert an if statement which employs file_exists to display a default image if no product image has been uploaded to the server.
This code does the trick for a page with only a single item:
PHP Code:$filename = "../../b/produkte/$k/$a.jpg";
if (file_exists($filename)) {
echo $Bild;
} else {
echo '<img src="http://elsibarita.de/b/produkte/kein_bild.gif" \>';
}
PHP Code:<?
$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\">" . stripslashes($row['Kleines_Bild']) . "</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></td>
</tr>
</table>
If it helps, here's how the results are displayed:
http://elsibarita.de/ud1.php?k=SO01
Using: MySQL 4.1.10a-Max
I'd be hugely grateful for any help. Thanks in advance.
PeteLast edited by HTML; 06-20-2006 at 07:05 AM.
-
06-19-2006, 04:58 AM #2
Re: How to insert IF statement into MySQL query displaying results in 2 columns?
It's a bit hard to read without formatting (see the tags supplied
) but at first glance it looks like you may have <table>...</table> within each and every cell.
If that's not by design, it could be where the problem lies.
-
06-19-2006, 06:30 AM #3
Re: How to insert IF statement into MySQL query displaying results in 2 columns?
Originally Posted by DeadMeatGF
Thanks for the reply. The table in every cell is by design. The code in the post all works fine on it's own, I just need to combine it somehow. I thinks what I basically need to know is if it's possible to insert the whole IF statement into the $item="..." part (which would supposedly be where stripslashes($row['Kleines_Bild']) is now - which fetches the link to the thumbnail). Here is is reformatted:
PHP Code:<?
$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\">" . stripslashes($row['Kleines_Bild']) . "</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>
</td>
</tr>
</table>Last edited by HTML; 06-20-2006 at 07:05 AM.
-
06-20-2006, 06:42 AM #4
Re: How to insert IF statement into MySQL query displaying results in 2 columns?
I've found a solution!.
For any other newbie's like me looking for a something similiar, here's my full code:
PHP 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>Last edited by HTML; 06-20-2006 at 07:06 AM.
-
06-20-2006, 08:20 AM #5
Re: How to insert IF statement into MySQL query displaying results in 2 columns?
Excellent - glad you found it
Cloudjiffy- PaaS for Developers
10-05-2020, 12:30 AM in Web Hosting Forum