2

I have a database that contains rows with links to each episode in them. I am trying to use the below code to get all the links for one type of show and I manage to do that successfully. What I can't seem to figure out is how I can get just one of the rows to display rather then having them all display in a single line of text so I could use them in building my page.

$query = "SELECT direct_link FROM episode_link WHERE anime_id = '$animeid'";
$result = mysql_query($query) or die(mysql_error());    
while($row = mysql_fetch_array($result)) 
{
    $dlink = $row['direct_link'];
    echo $dlink;
}

Currently it's displaying them all like this.

link1link2link3link4link5link6

I would like to be able to display them one at a time or put assign variables to them so I can put them in href tags on my site. I thought I could do this by using $row[1] but that does not display anything.

Sorry if I made it hard to understand, new to php so I am still learning.

7 Answers 7

2

I suggest you look up some basic tutorial sites abit more

$dlink = array();
while($row = mysql_fetch_array($result)){
        $dlink[] = $row['direct_link'];
}

then when you want to echo out:

foreach($dlink as $link){
echo '<a href="'.$link.'">'.$link.'</a><br />'.PHP_EOL;
}


Also: as you may want the title of the link and not a literal link, you should grab the title from the database too, if you have stored it that is:

SELECT direct_link,title FROM episode_link...

$dlink = array();
$i=0;
while($row = mysql_fetch_array($result)){
        $dlink[$i]['title'] = $row['title'];
        $dlink[$i]['link']  = $row['direct_link'];
$i++;
}

Then when echoing out:

foreach($dlink as $link){
echo '<a href="'.$link['link'].'">'.$link['title'].'</a><br />'.PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I am still trying to learn it all. I only started a day and a half ago :P My brains on the frits. Any you recommend? I have read been reading them and trying to use what they are teaching so it sticks more. Thanks for the help!
Theres alot to learn, Google is your friend. php.net/manual/en is also helpful ;p
1
while($row = mysql_fetch_array($result)) 
    {
        $dlink[] = $row['direct_link'];
    }

$count = $dlink;

echo $count;
echo $dlink[1]
echo $dlink[2]

and so on.

Comments

1
$dlink = array();
while($row = mysql_fetch_array($result)) 
    {
        $dlink[] = $row['direct_link'];
    }

to show it echo $dlink[0];

Comments

1
$dlink = array();

while ($row = mysql_fetch_assoc($result))
    $dlink[] = $row['direct_link'];

Comments

1

You can use something like this to return your results in a usable array.

// Returns the rows as an array of rows 
// Providing a key_column gives you access to specific rows (e.g. if (isset($result_array[$user_id])) ...) 
function db_result_array($result, $key_column = null) { 
   for ($array = array(); $row = mysql_fetch_assoc($result); isset($row[$key_column]) ?     $array[$row[$key_column]] = $row : $array[] = $row); 
   return $array; 
} 

Use:

$results = db_result_array(mysql_fetch_array($result),null)

Alternitively you can loop through mysql_fetch_array($result) and add the objects to an array, which is essentially what above function does.

$stored = array();
while($row = mysql_fetch_array($result)) 
{
    array_push($stored,$row['direct_link']);
}

print_r($stored); //Now $stored is an array of your direct_link values.

Comments

1

I'm not sure to really well understand your question but here something to give you inspiration :

$links = array();
while($row = mysql_fetch_array($result)) {
  $links[] = $row;
}

Then later you access each one by doing :

$links[1]['direct_link'];  // Or $links[2] ... 3 ... 4 depending on result count.

Or you could do :

foreach ($links as $link) {
  echo '<a href="'.$link['direct_link'].'">Link</a>';
}

Hope this help.

Comments

0

You can use array [] to add it in array.

$dlink = array();
while($row = mysql_fetch_array($result)) 
{
    $dlink[] = $row['direct_link'];
}

To print this

foreach($dlink as $arrays){
   echo $arrays; // This will print value on loop
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.