SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Having trouble with echo NULL values & trailing slash?

    Hi all, in the process of updating and changing my database tables but I'll getting into those other problems later..

    Ok so my problem is:

    PHP Code:
    echo "{$row1['dr2']} - <a href=\"gallery1/{$row1['event_id']}/{$row1['venue_id']}/{$row1['date']}\">{$row1['event_id']}{$row1['venue_id']}</a><br>\n "
    Which outputs below:

    example
    URL: ../event/venue/2007-09-27 (which works great..)

    Full code:

    PHP Code:
    <?php
        
    require_once('includes/mysql_connect.inc.php');
        
    $query "SELECT venue_id, event_id, DATE_FORMAT(date, '%d/%m/%y') AS dr2, date FROM gallery_details ORDER BY date DESC ";
        
    $res mysql_query($query);
        while(
    $row1 mysql_fetch_assoc($res))
        echo 
    "{$row1['dr2']} - <a href=\"gallery1/{$row1['event_id']}/{$row1['venue_id']}/{$row1['date']}\">{$row1['event_id']}{$row1['venue_id']}</a><br>\n ";
        
    ?>
    .. until my 'gallery_details' will have no venue or no event and stays NULL in my table and outputs:

    URL: ../event//2007-09-27
    or
    URL: ..//venue/2007-09-27

    how can i work around the trailing slash problem when venue_id or _event_id has no value?

    any suggestions?

    Thanks

  2. #2
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(isset($variable)){
    echo 
    "/{$variable}";

    going to be alot of if statments but it'll work

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the quick response baileylo, could you show an example and were to place this in the code?

    Thanks

  4. #4
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is how'd i'd do it.

    PHP Code:
    <?php
        
    require_once('includes/mysql_connect.inc.php');
        
    $query "SELECT venue_id, event_id, DATE_FORMAT(date, '%d/%m/%y') AS dr2, date FROM gallery_details ORDER BY date DESC ";
        
    $res mysql_query($query);
        while(
    $row1 mysql_fetch_assoc($res))
                 
    $Link "<a href='gallery1/{$row1['event_id']}/";
                 if(isset(
    $row1['venue_id']))
                      
    $Link .= $row1['venue_id'] . '/';
                 
    $Link .= "{$row1['date']}' >";
        echo 
    $row1['dr2'] . $Link "{$row1['event_id']}{$row1['venue_id']}</a><br>\n ";

        
    ?>
    And i believe that should do it, ad in more issets for other values that could be null.

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok so I've used the code above and now it just stays blank?? it doesn't show the links?

    Cheers

  6. #6
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by computerbarry View Post
    Ok so I've used the code above and now it just stays blank?? it doesn't show the links?

    Cheers
    Without looking at the code too much, it looks as if you need braces around the loop.
    PHP Code:
    <?php
    require_once('includes/mysql_connect.inc.php');
    $query "SELECT venue_id, event_id, DATE_FORMAT(date, '%d/%m/%y') AS dr2, date FROM gallery_details ORDER BY date DESC ";
    $res mysql_query($query);
    while(
    $row1 mysql_fetch_assoc($res))
    {
        
    $Link "<a href='gallery1/{$row1['event_id']}/";
        if(isset(
    $row1['venue_id']))
            
    $Link .= $row1['venue_id'] . '/';
        
    $Link .= "{$row1['date']}' >";
        echo 
    $row1['dr2'] . $Link "{$row1['event_id']}{$row1['venue_id']}</a><br>\n ";
    }
    ?>

  7. #7
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers trib4lmaniac, that fixed the problem but just back were i started, it still shows the trail slash:

    ../venue//2007-09-28 and ..//event/2007-09-28

    ??

    thanks

  8. #8
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just use:

    PHP Code:
    $url 'gallery1/ ' $row1['event_id'] . $row1['venue_id'] . $row1['date'];
    // does regex need /'s escaped? i cant remember
    $url preg_replace('~\/\/~''/'$url);

    echo 
    $row1['dr2'] . ' - <a href="' $url '">' $row1['event_id'] . $row1['venue_id'] . '</a><br />'
    PS: dont use double quotes and {} for variables - it makes it so hard to read and looks stupid.
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    One way to do it
    PHP Code:
    <?php

    require_once('includes/mysql_connect.inc.php');

    $query "SELECT venue_id, event_id, DATE_FORMAT(date, '%d/%m/%y') AS dr2, date FROM gallery_details ORDER BY date DESC";
    $res mysql_query($query);

    while (
    $row1 mysql_fetch_assoc($res)) {
        
        
    $link join('/'array_filter(
            array(
    'gallery1'$row1['event_id'], $row1['venue_id'], $row1['date'])
        ));

        
    $form '%s - <a href="%s">%s</a><br>';

        
    printf($form$row1['dr2'], $link$row1['event_id'] . $row1['venue_id']);

    }
    Or

    PHP Code:
    <?php

    require_once('includes/mysql_connect.inc.php');

    $query "SELECT venue_id, event_id, DATE_FORMAT(date, '%d/%m/%y') AS dr2, date FROM gallery_details ORDER BY date DESC";
    $res mysql_query($query);

    while (
    $row1 mysql_fetch_assoc($res)) {

        
    $link 'gallery1/' $row1['event_id'] . '/' $row1['venue_id'] . '/' $row1['date'];
        
    $link preg_replace('~/{2,}~''/'$link);
        
        
    $form '%s - <a href="%s">%s</a><br>';

        
    printf($form$row1['dr2'], $link$row1['event_id'] . $row1['venue_id']);

    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers guys, I've used logic_earth 2nd example, seems to work ok. I tried your example AlienDev works ok but theres no '/' between each variable.

    Would it be possible to add a couple of comments on logic_earth 2nd example?

    Thanks

    UPDATE:

    If theres an event and venue together, how could I add @ after the event (event@venue) like so, to the printf and not if it's only venue?

    Thanks

  11. #11
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    comments you mean describing whats going on?
    PHP Code:
    <?php

        
    // Build the link even with null values.
        
    $link 'gallery1/' $row1['event_id'] . '/' $row1['venue_id'] . '/' $row1['date'];

        
    // Clean up the link strip out excess forward slashes (//, ////).
        
    $link preg_replace('~/{2,}~''/'$link);

        
    // How should it look when we output. %s = string data, Three postions total.
        
    $form '%s - <a href="%s">%s</a><br>';

        
    // Take the format above and process it using the below function inserting
        // the data of our chocie in the postion of our choice.
        
    printf($form$row1['dr2'], $link$row1['event_id'] . $row1['venue_id']);
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  12. #12
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    comments you mean describing whats going on?
    Yes. Thanks alot logic_earth

    Just one last thing:

    How could I add @ after the event (event@venue) like so if both columns are present to to the printf output that is? It currently shows eventvenue?

    PHP Code:
    // Take the format above and process it using the below function inserting
        // the data of our chocie in the postion of our choice.
        
    printf($form$row1['dr2'], $link$row1['event_id'] . $row1['venue_id']); 
    Thanks again

  13. #13
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Simple just change $row1['event_id'] . $row1['venue_id']
    To $row1['event_id'] . '@' . $row1['venue_id']

    Or
    PHP Code:
    // Four postions, added 1 extra (%s-@-%s)
    $form '%s - <a href="%s">%s@%s</a><br>';
    printf($form$row1['dr2'], $link$row1['event_id'], $row1['venue_id']); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  14. #14
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks logic_earth. the first example works but I only need it to show when there is an event@venve.

    It shows event@venue but it also shows @venue when theres no event, plus if theres just event it shows @event, I only want it to show like I said when event has a venue, making sense?

    there second example gave an error:
    Code:
    Warning: printf() [function.printf]: Too few arguments in .../mysite/.. etc
    thanks again

  15. #15
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    require_once('includes/mysql_connect.inc.php');

    $query "SELECT venue_id, event_id, DATE_FORMAT(date, '%d/%m/%y') AS dr2, date FROM gallery_details ORDER BY date DESC";
    $res mysql_query($query);

    while (
    $row1 mysql_fetch_assoc($res)) {

        
    // Build the link even with null values.
        
    $link 'gallery1/' $row1['event_id'] . '/' $row1['venue_id'] . '/' $row1['date'];

        
    // Clean up the link strip out excess forward slashes (//, ////).
        
    $link preg_replace('~/{2,}~''/'$link);

        
    // If both event_id and venue_id are not empty then add @ between them.
        
    if (!empty($row1['event_id']) && !empty($row1['venue_id'])) {
            
    $text $row1['event_id'] . '@' $row1['venue_id'];
        } else {
            
    $text $row1['event_id'] . . $row1['venue_id'];
        }

        
    // How should it look when we output. %s = string data, Three postions total.
        
    $form '%s - <a href="%s">%s</a><br>';

        
    // Take the format above and process it using the below function inserting
        // the data of our chocie in the postion of our choice.
        
    printf($form$row1['dr2'], $link$text);

    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  16. #16
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Big thanks

    works great now, gave an error at first:
    PHP Code:
    } else {
            
    $text $row1['event_id'] . . $row1['venue_id'];
        } 
    just deleted .

    PHP Code:
    } else {
            
    $text $row1['event_id'] . $row1['venue_id'];
        } 
    solved the problem

    Thanks again logic_earth

    If you get the chance have a look at:
    Need help with SELECT & WHERE? its the next step from the above just having a little bit of trouble, cheers


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
  •