...

View Full Version : Displaying and Not Displaying SQL data



sonofjack
09-01-2011, 03:53 PM
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?



$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

m1l
09-01-2011, 06:07 PM
Something like this




$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();
}
?>

sonofjack
09-01-2011, 06:27 PM
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'



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





$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();
}
?>

Inigoesdr
09-02-2011, 05:14 AM
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:

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

sonofjack
09-02-2011, 09:37 AM
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



<?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

sonofjack
09-02-2011, 01:27 PM
Solved it.



if ($row['gigsetlist']=="")

should have been



if ($row['gigsetlist']!=="")


Thanks all for the help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum