78 users online (1 members and 77 guests)  


  Results 1 to 5 of 5

Related

  1. perl and mysql-compare 2 columns    Forum: CGI Perl Forum
    Replies: 7
  2. need help displaying results with links    Forum: PHP Forum
    Replies: 0
  3. PHP displaying search results    Forum: PHP Forum
    Replies: 24
  4. Displaying Paged Results In A Table    Forum: Javascript Forum
    Replies: 4
  5. Replies: 0
  1. #1
    petewalton's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2006
    Posts
    3

    Question 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" \>';

    But if having all sorts of problems trying to wire it into my results page with 2 columns:

    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>
    I (think I) need to include the if statement inside the '$item=' part where it takes the link to the thumbnail from the database stripslashes($row['Kleines_Bild']) but I just can't work out how. I've even tried putting the whole '$item=' inside the if statment but that either displays every item with the default image or none at all - because it's referring to '$filename=' just once before diplaying the results.
    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.
    Pete
    Last edited by HTML; 06-20-2006 at 07:05 AM.

  2. #2
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    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.

  3. #3
    petewalton's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2006
    Posts
    3

    Re: How to insert IF statement into MySQL query displaying results in 2 columns?

    Quote Originally Posted by DeadMeatGF
    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.
    Hi 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.

  4. #4
    petewalton's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2006
    Posts
    3

    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.

  5. #5
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: How to insert IF statement into MySQL query displaying results in 2 columns?

    Excellent - glad you found it