SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 38
  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP Check to see if a field is blank before outputting data

    Hello,
    I ahve the following code:

    PHP Code:
    <!--Cost-->
        <?php
            $sql 
    "SELECT atm_rides.ride_cost
                    FROM atm_rides
                    WHERE atm_rides.ride_id="
    .${$method}[$item.'_id']."";

            
    $result mysql_query($sql);
            
            if (
    mysql_num_rows($result) >= 1) {
        
    ?>
        
                <tr>
                    <th>
                        <h2>
                            Ride Cost
                        </h2>
                    </th>
                    <td>
                        <?php
                            
    while ($getstatdata mysql_fetch_array($result)) {
                                echo 
    sprintf('%s%s<br />''&pound;'$getstatdata['ride_cost']);
                            }
                        
    ?>
                    </td>
                </tr>
        
        <?php
            
    } else {
                echo 
    null;
            }
        
    ?>
    <!--End Cost-->

    The part of the code I need help with is the one below:

    PHP Code:
            if (mysql_num_rows($result) >= 1) { 
    I thought that that would distinguish if the field I am chcking has something in it or not. Basically if the field for what this line in a larger table is about is null then I don;t want anything outputted but if the field does have soemthing in then I want a row to be shown in the larger table.

    Any help please?

    Thanks

    Regards,
    Neil

  2. #2
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hello,

    I ahve just tried the following:

    PHP Code:
    <!--Cost-->
        <?php
            $sql 
    "SELECT atm_rides.ride_cost
                    FROM atm_rides
                    WHERE atm_rides.ride_id="
    .${$method}[$item.'_id']."";

            
    $result mysql_query($sql);
            
            
    $getstatdata mysql_fetch_array($result)
            
            if (
    count($getstatdata) >= 1) {
        
    ?>
        
                <tr>
                    <th>
                        <h2>
                            Ride Cost
                        </h2>
                    </th>
                    <td>
                        <?php
                            
    foreach($getstatdata as $stat {
                                echo 
    sprintf('%s%s<br />''&pound;'$stat);
                            }
                        
    ?>
                    </td>
                </tr>
        
        <?php
            
    } else {
                echo 
    null;
            }
        
    ?>
    <!--End Cost-->
    but that doesn;t work. No errors either.

    Please help.

    Thanks

    Regards,
    Neil

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    if (!is_null($getstatdata['ride_cost']))

    http://www.php.net/manual/en/function.is-null.php

  4. #4
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    This:

    PHP Code:
    <!--Cost-->
        <?php
            $sql 
    "SELECT atm_rides.ride_cost
                    FROM atm_rides
                    WHERE atm_rides.ride_id="
    .${$method}[$item.'_id']."";

            
    $result mysql_query($sql);
            
            
    $getstatdata mysql_fetch_array($result);
            
            if (!
    is_null($getstatdata['ride_cost'])) {
        
    ?>
        
                <tr>
                    <th>
                        <h2>
                            Ride Cost
                        </h2>
                    </th>
                    <td>
                        <?php
                            
    foreach($getstatdata as $stat {
                                echo 
    sprintf('%s%s<br />''&pound;'$stat);
                            }
                        
    ?>
                    </td>
                </tr>
        
        <?php
            
    } else {
                echo 
    null;
            }
        
    ?>
    <!--End Cost-->
    Doesn;t seem to work and no error is being displayed

  5. #5
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, instead of using is_null(), is there a way I can see if the $getstatdata array has value in it. If it does then output the whole table row otherwise don;t output anything. As in sometimes throguh this stats table the array will only contain 1 value like ride_cost (a ride can only have 1 cost) but sometimes might contain might contain multiple values such as ride_area (a ride such as a train belongs in multiple areas) therefore multiple values in the array.

    Thanks

    Neil

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    How many lines do you expect to get from your query? Always 1? 0, 1 or many?

    If it's always 1 line, then try this:
    PHP Code:
    <!--Cost-->
        <?php
            $sql 
    "SELECT atm_rides.ride_cost
                    FROM atm_rides
                    WHERE atm_rides.ride_id="
    .${$method}[$item.'_id']."";
            
    $result mysql_query($sql);
            
    $getstatdata mysql_fetch_array($result);
            if (!
    is_null($getstatdata['ride_cost'])) {
        
    ?>
                <tr>
                    <th>
                        <h2>
                            Ride Cost
                        </h2>
                    </th>
                    <td>
                        <?php
                                
    echo sprintf('%s%s<br />''&pound;'$getstatdata['ride_cost']);
                        
    ?>
                    </td>
                </tr>
        <?php
            
    } else {
                echo 
    null;
            }
        
    ?>
    <!--End Cost-->

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rctneil View Post
    Also, instead of using is_null(), is there a way I can see if the $getstatdata array has value in it. If it does then output the whole table row otherwise don;t output anything. As in sometimes throguh this stats table the array will only contain 1 value like ride_cost (a ride can only have 1 cost) but sometimes might contain might contain multiple values such as ride_area (a ride such as a train belongs in multiple areas) therefore multiple values in the array.

    Thanks

    Neil
    With mysql_num_rows you can check how many rows your query returned.
    Any row returned always contains all fields you selected in your query.
    Since you select only one field (ride_cost), it'll always and only contain that field. The value of that field might be NULL, but the field itself will still be there in the array.

  8. #8
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hello,
    I ahve just come back to trying to sort this out and I am getting a little confused. My aim is to create a small block of code which can output a line in a statistics table but be able to do it if the sql query returns 1 line or multiple lines.

    For example, the query below:

    PHP Code:
    $sql "SELECT atm_areas.area_name
                    FROM atm_areas, atm_rides, atm_area_ride
                    WHERE atm_rides.ride_id = "
    .${$method}[$item.'_id']."
                    AND atm_rides.ride_id = atm_area_ride.ride_id
                    AND atm_areas.area_id = atm_area_ride.area_id"
    ;

            
    $result mysql_query($sql); 
    That query when run for some of the inputted id's will return multiple rows for the field area_name (like a train in multiple areas) but other id's will only return 1 row for an area_name (like a carousel - only in 1 area at a time)

    If I run the above query in my sql program - Navicat but substitute:

    PHP Code:
    ".${$method}[$item.'_id']." 
    for an id of 23 then I get the following output:

    area_name
    -----------
    Cred Street
    Forbidden Valley
    Mutiny Bay

    If I run this query in my page so the code substitued above does equal 23 (so i should get the same query results) and then run the following lines:

    PHP Code:
            $getstatdata mysql_fetch_array($result);
            
            
    print_r($getstatdata); 
    I get this outputted:

    Array ( [0] => Cred Street [area_name] => Cred Street )

    Why?

    What is going wrong?

    Thanks

    Regards,
    Neil

  9. #9
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Nothing is going wrong. With this line
    PHP Code:
    $getstatdata mysql_fetch_array($result); 
    you get the first of the three lines returned by your query. This line contains only one field: area_name.
    To get the other two records, you'll have to loop through the mysql result array using a while-loop:
    PHP Code:
    $sql "SELECT atm_areas.area_name
                    FROM atm_areas, atm_rides, atm_area_ride
                    WHERE atm_rides.ride_id = "
    .${$method}[$item.'_id']."
                    AND atm_rides.ride_id = atm_area_ride.ride_id
                    AND atm_areas.area_id = atm_area_ride.area_id"
    ;
    $result mysql_query($sql); 
    while (
    $getstatdata mysql_fetch_array($result)) {
      
    print_r($getstatdata);


  10. #10
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Thankyou for that,

    I now ahve the following code block:

    PHP Code:
    <!--Location-->
        <?php
            $sql 
    "SELECT atm_areas.area_name
                    FROM atm_areas, atm_rides, atm_area_ride
                    WHERE atm_rides.ride_id = "
    .${$method}[$item.'_id']."
                    AND atm_rides.ride_id = atm_area_ride.ride_id
                    AND atm_areas.area_id = atm_area_ride.area_id"
    ;

            
    $result mysql_query($sql);
            
        
    ?>
                <tr>
                    <th>
                        <h2>
                            Location
                        </h2>
                    </th>
                    <td>
                        <?php
                            
    while ($getstatdata mysql_fetch_array($result)) {
                                echo 
    sprintf('%s<br />'$getstatdata['area_name']);
                            }
                        
    ?>
                    </td>
                </tr>
    <!--End Location-->
    How can I make the antire stat row not be displayed if the query returns no rows

  11. #11
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Around this bit:

    PHP Code:
    <?php
        
    while ($getstatdata mysql_fetch_array($result)) {
            echo 
    sprintf('%s<br />'$getstatdata['area_name']);
        }
    ?>
    Put a conditional like this:
    PHP Code:
    <?php
        
    if(mysql_num_rows($result) >= 1) {
            while (
    $getstatdata mysql_fetch_array($result)) {
                echo 
    sprintf('%s<br />'$getstatdata['area_name']);
            }
        }
        else {
            echo 
    'No data to display.';
        }
    ?>
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  12. #12
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorted:

    Final Code:

    PHP Code:
    <!--Location-->
        <?php
            $sql 
    "SELECT atm_areas.area_name
                    FROM atm_areas, atm_rides, atm_area_ride
                    WHERE atm_rides.ride_id = "
    .${$method}[$item.'_id']."
                    AND atm_rides.ride_id = atm_area_ride.ride_id
                    AND atm_areas.area_id = atm_area_ride.area_id"
    ;

            
    $result mysql_query($sql);
            
            if (
    mysql_num_rows($result) > 0) {
            
        
    ?>
                <tr>
                    <th>
                        <h2>
                            Location
                        </h2>
                    </th>
                    <td>
                        <?php
                            
    while ($getstatdata mysql_fetch_array($result)) {
                                echo 
    sprintf('%s<br />'$getstatdata['area_name']);
                            }
                        
    ?>
                    </td>
                </tr>
                
        <?php
            
    } else {
                echo 
    null;
            }
        
    ?>
    <!--End Location-->
    Edit: Must ahve posted at the same time as you. Thanks anyway

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Neil - quick tip (and please don't take offense).

    Open notepad and type 'have' precisely 100 times. If you have more than 15 mistakes, do it again.

    I used to make alot of typos for typing too fast, but when you pick up on the consistent words it's an easy fix, just practice doing it right.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  14. #14
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeh, I hate typing that word. Everytime I get it wrong and I guess you HAVE noticed it throughout my posts on this forum

    :-)

    Thanks

  15. #15
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I must admit that it is rather amusing reading back through this thread and seeing all the mistakenly spelt haves!

    I have just copied the block of code that I ended up with and pasted it in the same page to make another row for another statistic but it causes the whole page to fail with no error:

    PHP Code:
        <?php
            $sql 
    "SELECT atm_rides.ride_name
                    FROM atm_rides
                    WHERE atm_rides.ride_id = "
    .${$method}[$item.'_id']."");
            
    $result mysql_query($sql);
            if (
    mysql_num_rows($result) >= 1) {
        
    ?>
                <tr>
                    <th>
                        <h2>
                            Name
                        </h2>
                    </th>
                    <td>
                        <?php
                            
    while ($getstatdata mysql_fetch_array($result)) {
                                echo 
    sprintf('%s<br />'$getstatdata['ride_name']);
                            }
                        
    ?>
                    </td>
                </tr>
        <?php
            
    } else {
                echo 
    null;
            }
        
    ?>

  16. #16
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A completely blank output usually means you have a fatal error, or parse error. Of course, this assumes your script should generate output under any condition. The code you posted won't.

    Turn on display_errors in .htaccess or php.ini so you can find your error.
    You can also look in php's error log file.

  17. #17
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get a completley blank screen. I think an error is being shown but I cannot see it due to the fact that the code erroring out is inside a tab script. How do I access and view the PHP Error Log to find out what the error was which I can't see?

  18. #18
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rctneil View Post
    I don't get a completley blank screen. I think an error is being shown but I cannot see it due to the fact that the code erroring out is inside a tab script.
    right click>view source? This should be something you do all the time. Browsers render html output, which hides the true output.

  19. #19
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found the error in the PHP error log and it was saying that I had an unexpected ")" on line 16 so i checked and removed the offender and now all is fine!

    I hate things like that but at least I now know where the PHP Error Log is!

    Thanks

    Regards,
    Neil

  20. #20
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, I was updating more of the stat lines in that same file by repeating that code block and changing specific parts of it so I did a line for ride_cost and when I viewed the page it showed perfectly so then I decided to see if the entire cost line would dissapear if I emptied the the ride_cost field for that specific ride in the database so I emptied it and refreshed the page but the line still showed but where the cost should have been displayed it was just empty. Is this an issue with the code or the database?

  21. #21
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rctneil View Post
    Right, I was updating more of the stat lines in that same file by repeating that code block and changing specific parts of it so I did a line for ride_cost and when I viewed the page it showed perfectly so then I decided to see if the entire cost line would dissapear if I emptied the the ride_cost field for that specific ride in the database so I emptied it and refreshed the page but the line still showed but where the cost should have been displayed it was just empty. Is this an issue with the code or the database?
    I already explained that to you:
    Any row returned always contains all fields you selected in your query.
    Since you select only one field (ride_cost), it'll always and only contain that field. The value of that field might be NULL or empty (spaces), but the field itself will still be there in the array.
    So, you'll have to take care of it in your code.

  22. #22
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would I have to add an && to the following if statement?

    if ((mysql_num_rows($result) >= 1) && ) {

    Because I can't figure out what I am supposed to put in to overcome this issue

  23. #23
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No.

    I'm assuming $result outputs an error, what if you put:
    PHP Code:
    $result mysql_query($sql) or die(mysql_error()); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  24. #24
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    strlen()
    trim()

  25. #25
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No error is being outputted?


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
  •