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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.