SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A condition in a mysql_fecth_array

    I'm trying to do a mysql_fetch_array with condition in it and was wondering where is the best way to apply the condition.
    Inside the array or as a function.

    I think that my code will be little more explicit, I hope

    PHP Code:
                        <?php
                            $sql 
    'SELECT * FROM pv ORDER BY region ASC';
                            
    $resultat mysql_query$sql );

                            while (
    $row mysql_fetch_array($resultat)) {

                            
    $lieu $row['lieu'];
                            
    $adresse $row['adresse'];
                            
    $url $row['url'];
                            
    $region $row['region'];
                            
    $ville $row['ville'];
                            
    $info $row['info'];
                            
    $tel $row['tel'];
                            
    $url $row['url'];
                            
    $site '<a href=http://'.$url.' target="_blank">Site internet</a>';                        
                            
    $www = if (!empty($url)) {
                            echo 
    $site;
                            };
                            
                                echo 
    "<div class='desbeer'>";
                                echo 
    "<span class='beer'>";
                                echo 
    $row ['lieu'];
                                echo 
    "</span><br/>";
                                echo 
    "<span class='type'>";
                                echo 
    $row ['adresse'];
                                echo 
    "<br />";
                                echo 
    "T&eacute;l.: ";
                                echo 
    $tel;
                                echo 
    "<br />";                    
                                echo 
    $region;
                                echo 
    "</span><br>";
                                echo 
    "<span class='descbeerPh'>";
                                echo 
    $row['ville'];
                                echo 
    "<br />";
                                echo 
    $www;
                                echo 
    "<div>";
                                echo 
    $info;
                                echo 
    "</div>";
                                echo 
    "</span><br>";
                                echo 
    "</div>";
                            }

                    
    ?>
    I know it's a mess but I want to have the "$www" printing only if is not empty??

  2. #2
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($url != '') echo '<a href=http://'.$url.' target="_blank">Site internet</a>';

    $str = '';
    $str .= '<div class="desbeer">';
    $str .= '<span class="beer">';
    // other lines
    echo $str;

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow!
    Much easier than I thought!

    Thank you
    --------Solved---------

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Instead of writing all this:
    PHP Code:
    $lieu $row['lieu'];
    $adresse $row['adresse'];
    $url $row['url'];
    $region $row['region'];
    $ville $row['ville'];
    $info $row['info'];
    $tel $row['tel'];
    $url $row['url']; 
    One can just do:
    PHP Code:
    extract($row); 
    But there is a catch, you don't want to do this just anywhere. Because it could over write other variables. But if you move it into a function then it is no longer an issue.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
                                echo "<div class='desbeer'>";
                                echo 
    "<span class='beer'>";
                                echo 
    $row ['lieu'];
                                echo 
    "</span><br/>";
                                echo 
    "<span class='type'>";
                                echo 
    $row ['adresse'];
                                echo 
    "<br />";
                                echo 
    "T&eacute;l.: ";
                                echo 
    $tel;
                                echo 
    "<br />";                    
                                echo 
    $region;
                                echo 
    "</span><br>";
                                echo 
    "<span class='descbeerPh'>";
                                echo 
    $row['ville'];
                                echo 
    "<br />";
                                echo 
    $www;
                              echo 
    "<div>";
                                echo 
    $info;
                                echo 
    "</div>";
                                echo 
    "</span><br>";
                                echo 
    "</div>"
    Instead of all that, working out whats single and double quotes etc look at using the heredoc syntax www.php.net/heredoc

    PHP Code:

    $output 
    EOL<<<
    <
    div class='desbeer'>
    <
    span class='beer'$row['lieu'] </span>
    <
    br/>
     <
    span class='type'$row['adresse'] <br /> T&eacute;l.: 
     
    $tel <br />$region </span><br>
    <
    span class='descbeerPh'$row['ville'] <br />
    $www 
    <div$info  </div></span><br> </div>
    EOL;

    echo 
    $output
    and it respects double quotes!

    Then you can echo your $output as and when it suits you, maybe after another sql call etc.

    You can also truly see how many nested divs and spans you are using.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •