Web Analytics Made Easy -
StatCounter Displaying and Not Displaying SQL data - CodingForum

Announcement

Collapse
No announcement yet.

Displaying and Not Displaying SQL data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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();
    }
    ?> 

    Comment


    • #3
      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();
      }
      ?>

      Comment


      • #4
        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();

        Comment


        • #5
          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

          Comment


          • #6
            Solved it.

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

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

            Comment

            Working...
            X
            😀
            🥰
            🤢
            😎
            😡
            👍
            👎