Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts

    Displaying and Not Displaying SQL data

    Hi

    I can't find any solution to this one but hopefully someone on here might know.

    I have a page that displays a list of gigs and I want one field to show up as a link only if something has been entered into that field.
    I can set it up so it shows the field data which relates to the filename it links to, but I want it to display an image or something else of my choice (eg 'SETLIST') with the entered data acting as a hyperlink as I have it now.

    I wondered about a if ...else statement in here to say if the SETLIST field has something in it then display this code... otherwise display this code.
    But I can't work it out

    Can anyone help please?

    [gigsetlist] is the SQL field that I type 19950830 into it so it links to a file called 19950830.php and opens accordingly. But some of these in the list won't have anything so I don't want to a link to appear.
    However I don't want the link to be displayed as 19950830 but instead SETLIST or an icon.
    Hope that makes sense. Can anyone help please?

    Code:
    $sql = "SELECT id, gigdate,  date_format(gigdate,'%d %M') as d, gigshortdesc, gigsetlist FROM gigs WHERE gigdate 
    
    BETWEEN '1995-01-01' AND '1995-12-31' ORDER BY gigdate ASC";
    
    
    
    print '<table>';
    
    foreach($dbh->query($sql) as $row)
    
    { 
    print '<tr>
    <td width=100>' . $row['d']  . ' </td>
    <td> ' . $row['gigshortdesc'] . '</td>
    <td> <a href="sets/' . $row['gigsetlist'] . '.php" onclick="return openWin(this.href, this.target, 480, 480, 1, 0, 0, 0, 0, 1);" 
    
    target="_setlist">' . $row['gigsetlist'] . '</a></td>
    </tr>';
    
    }
    
    print '</table>';
    
    $dbh = null;
    }
    
    catch(PDOException $e)
    {
    echo $e->getMessage();
    }
    ?>
    Many thanks

    sonofjack

  • #2
    m1l
    m1l is offline
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post
    Something like this



    PHP Code:
    $sql "SELECT id, gigdate,  date_format(gigdate,'%d %M') as d, gigshortdesc, gigsetlist FROM gigs WHERE gigdate 

    BETWEEN '1995-01-01' AND '1995-12-31' ORDER BY gigdate ASC"
    ;



    print 
    '<table>';

    foreach(
    $dbh->query($sql) as $row)


    print 
    '<tr>
    <td width=100>' 
    $row['d']  . ' </td>
    <td> ' 
    $row['gigshortdesc'] . '</td>
    <td> '
    ;
    if (
    $row['gigsetlist']=="")
    {
       print 
    '&nbsp;';
    }
    else
    {
      print 
    '<a href="sets/' $row['gigsetlist'] . '.php" onclick="return openWin(this.href, this.target, 480, 480, 1, 0, 0, 0, 0, 1);" 

    rel="nofollow" target="_setlist">' 
    $row['gigsetlist'] . '</a>';
    }
    print 
    '</td>
    </tr>'
    ;

    }

    print 
    '</table>';

    $dbh null;
    }

    catch(
    PDOException $e)
    {
    echo 
    $e->getMessage();
    }
    ?> 

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts
    Thanks for the reply.
    However it doesn't seem to work. At first it displayed an error
    <b>Parse error: syntax error, unexpected '<' in</b>...............

    and I noticed the code was missing another 'print'

    Code:
    if ($row['gigsetlist']=="")
    {
    print '<a href="sets/' . $row['gigsetlist'] . '.php" onclick="return openWin(this.href, this.target, 480, 480, 1, 0, 0, 0, 0, 1);"......
    But now I'm getting this error...
    <b>Parse error: syntax error, unexpected T_CATCH in</b>..............

    Is there something else missing somewhere? Cheers



    Code:
    $sql = "SELECT id, gigdate,  date_format(gigdate,'%d %M') as d, gigshortdesc, gigsetlist FROM gigs WHERE gigdate 
    
    BETWEEN '1995-01-01' AND '1995-12-31' ORDER BY gigdate ASC";
    
    
    
    print '<table>';
    
    foreach($dbh->query($sql) as $row)
    
    { 
    print '<tr>
    <td width=100>' . $row['d']  . ' </td>
    <td> ' . $row['gigshortdesc'] . '</td>
    <td> ';
    if ($row['gigsetlist']=="")
    {
    print '<a href="sets/' . $row['gigsetlist'] . '.php" onclick="return openWin(this.href, this.target, 480, 480, 1, 0, 0, 0, 0, 1);" 
    
    rel="nofollow" rel="nofollow" target="_setlist">' . $row['gigsetlist'] . '</a></td>
    </tr>';
    
    }
    
    
    print '</table>';
    
    $dbh = null;
    }
    
    catch(PDOException $e)
    {
    echo $e->getMessage();
    }
    ?>

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    I'm guessing you copied this from a tutorial or something and they had it in a try/catch block. All you have is the catch portion, which is causing the syntax error. Just remove this part:
    PHP Code:
    catch(PDOException $e)
    {
    echo 
    $e->getMessage();


  • #5
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts
    Thanks for posting.
    My error, there is a <b>try</b> bit that I omitted in the code I posted above.


    Some bits are from tutorials. I'm new to the PDO stuff after it being a recommended path but I'm still trying to learn it.

    The full code for the section is

    [code]
    <?php

    try {

    require_once "pdo_testdb_connect.php";


    $dbh = testdb_connect ();


    $sql = "SELECT id, gigdate, date_format(gigdate,'%d %M') as d, gigshortdesc, gigsetlist FROM gigs WHERE gigdate

    BETWEEN '1995-01-01' AND '1995-12-31' ORDER BY gigdate ASC";



    print '<table>';

    foreach($dbh->query($sql) as $row)

    {
    print '<tr>
    <td width=100>' . $row['d'] . ' </td>
    <td> ' . $row['gigshortdesc'] . '</td>
    <td> ';

    if ($row['gigsetlist']=="")
    {
    print '<a href="sets/' . $row['gigsetlist'] . '.php" onclick="return openWin(this.href, this.target, 480, 480, 1, 0, 0, 0, 0, 1);"

    rel="nofollow" rel="nofollow" target="_setlist">' . $row['gigsetlist'] . '</a></td>
    </tr>';

    }


    print '</table>';

    $dbh = null;
    catch(PDOException $e)
    {
    echo $e->getMessage();
    }


    }


    ?>

    Is there another reason why I get "syntax error, unexpected T_CATCH in.." error message for the above code?

    I can see how the
    Code:
    if ($row['gigsetlist']=="")
    {
    print '<a href="sets/' . $row['gigsetlist'] . '.php" onclick="return openWin(this.href, this.target, 480, 480, 1, 0, 0, 0, 0, 1);" 
    ....etc
    should work as suggested earlier but is this causing some conflict and missing something else?
    Many thanks

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Posts
    153
    Thanks
    34
    Thanked 0 Times in 0 Posts
    Solved it.

    Code:
    if ($row['gigsetlist']=="")
    should have been

    Code:
    if ($row['gigsetlist']!=="")
    Thanks all for the help.


  •  

    Posting Permissions

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