Jump to content

[SOLVED] An array from the database, creating links and stuff....


suttercain

Recommended Posts

Hi guys,

 

Right now I am using this code:

<?php
if (isset($_GET['id'])) {
    $sql = "SELECT characters FROM comics WHERE comic_id = '" . mysql_real_escape_string($_GET['id']) . "'";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
      }
    }
  }

if ($row['characters'] == !NULL) {
		  $people = explode (", ", $row['characters']);
		  echo "<b>CHARACTER(S):</b><br><font color='#263B5A'>" ;
		  sort ($people);
		  foreach ($people as $key=> $val) {
		  $val_escaped = mysql_real_escape_string($val);
		  $rez = mysql_query ("SELECT name, character_id FROM characters WHERE name =  '$val_escaped'") or die(mysql_error());
		  while ($row = mysql_fetch_assoc($rez)){
		  echo "<a href='../view_characters.php?id={$row['character_id']}'>" . $val . "</a> ";
				}
			}

?>

 

What this code does:

I have a column in my MySQL table that contains characters that appear in a specific comic book. The characters a seperated by a comma. EXP: Superman, Lois Lane, Green Lantern, Perry White. This code echos the characters and allows the user to click on, let's say Superman, and view his profile.

 

The problem I am having:

Let's say I have Superman, Lois Lane, Green Lantern, Perry White in that cell. If I don't have a profile for Perry White, it won't echo his name in the character list.

 

My Desired results:

If there is no profile for a character, instead of not showing the name at all, is it possible to just have that character not "clickable"? So Superman, Lois Lane, Green Lantern would be links because they have a profile, but Perry White would be echoed, just not a link?

 

Example Page:

http://www.supermandatabase.com/comics/view_comics.php?id=4515

The character section is at the very bottom.

 

Thank you for your advice and help.

 

 

Can you do something like:

 

 

  
<?php ......

while ($row = mysql_fetch_assoc($rez))
{
    if ($row['character_id'] == "")
    { echo $val; }
    else
    { echo "<a href='../view_characters.php?id={$row['character_id']}'>" . $val . "</a> "; }
}

 

??

 

Cheers,

tdw

okay, sorry i didn't read properly...

 

try

 

 

<?php...

$rez = mysql_query ("SELECT name, character_id FROM characters WHERE name =  '$val_escaped'") or die(mysql_error());
if (mysql_num_rows($rez)==0)
{ echo $val; }
else
$row = mysql_fetch_assoc($rez));
echo "<a href='../view_characters.php?id={$row['character_id']}'>" . $val . "</a> ";
}

 

might work ??

couple of errors in my swift typing

 

try

 

<?php...

$rez = mysql_query ("SELECT name, character_id FROM characters WHERE name =  '$val_escaped'") or die(mysql_error());
if (mysql_num_rows($rez)==0)
{ echo $val; }
else
{
$row = mysql_fetch_assoc($rez);
echo "<a href='../view_characters.php?id={$row['character_id']}'>" . $val . "</a> ";
}

 

that should give a least something :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.