1

How can I display array values that I retrieved from the database for example the following array values.

while($row = mysqli_fetch_array($query)){
    $post_id[] = $row['id'];
    $post_user_id[] = $row['user_id'];
    $post_title[] = $row['title'];
}

Output.

<li><a href="' . $post_user_id . 'post.php?id="' . $post_id . '">' . $post_title . '</a></li>
<li><a href="' . $post_user_id . 'post.php?id="' . $post_id . '">' . $post_title . '</a></li>
<li><a href="' . $post_user_id . 'post.php?id="' . $post_id . '">' . $post_title . '</a></li>
<li><a href="' . $post_user_id . 'post.php?id="' . $post_id . '">' . $post_title . '</a></li>
<li><a href="' . $post_user_id . 'post.php?id="' . $post_id . '">' . $post_title . '</a></li>

6 Answers 6

2

You can do the following outside the while loop:

for($i=0;$i<count($post_id);$i++) {
    echo '<li><a href="'.$post_user_id[$i].'post.php?id='.$post_id[$i].'">'.$post_title[$i] . '</a></li>';
}
Sign up to request clarification or add additional context in comments.

5 Comments

All your lines will be the same. Where $i?
sizeof() more faster than count()
Check this with firebug. Or you can read about operations speed on phpbench.com
@Alexander honestly, this site makes people totally misunderstood performance issues. It makes people think that difference between count() and sizeof() (if it really was, between just different names of single function) does matter anything. While it doesn't. And you are right with firebug. Just always test everything with firebug. When network latency involved, nothing of such a microoptimization could be noticeable. Trust me, no code itself does affect performance. Data manipulation does affect it.
@Alexander Shrapnel is right, but to put it in less aggressive terms: sizeof: Description: "This function is an alias of: count()." Please provide irrefutable proof that an alias of a function is faster than the function itself. Thank you. :)
1
<?
$data = array();
while($row = mysqli_fetch_array($query)){
  $data[] = $row;
}
?>

Output.

<? foreach ($data as $row): ?>
<li>
 <a href="<?=$row['post_user_id']?>post.php?id=<?=$row['post_id']?>">
  <?=$row['post_title']?>
 </a>
</li>
<? endforeach ?>

Comments

0
for ($i=0; $i<sizeof($post_id); $i++) {
   echo '<li><a href="' . $post_user_id[$i] . 'post.php?id=' . $post_id[$i] . '">' 
   . $post_title[$i] . '</a></li>';
}

Comments

0
while($row = mysqli_fetch_array($query)){
    $post_id = $row['id'];
    $post_user_id = $row['user_id'];
    $post_title = $row['title'];
    echo "<li><a href=\"" . $post_user_id . "post.php?id=" . $post_id . "\">" . $post_title . "</a></li>";
}

But in my Oppinion, the Links are not very well formated, you would need a [userid]post.php for each user. Better would be:

    echo "<li><a href=\""post.php?userid=\"".$post_user_id."\"&id=". $post_id ."\">" . $post_title . "</a></li>";

So your post.php gets the userid and the id per GET by clicking on the link.

2 Comments

I dont want to use it in the same while loop.
$post_id[] is array. $post_id - is not array.
0

Instead of splitting up a database record (a row) across multiple variables, it's usually a better idea to keep them together in a multi-dimensional array like this:

$posts = array();

while ($row = mysqli_fetch_array($query)) {
    $posts[] = $row;
}

foreach ($posts as $post) {
    echo $post['id'];
    echo $post['user_id'];
    echo $post['title'];
    ...
}

Comments

0

Better way would be

while($row = mysqli_fetch_array($query)){
   echo '<li><a href="' . $row['user_id'] . 'post.php?id="' . $row['id'] . '">' . $row['title'] . '</a></li>';
}

if you need the same output. But in my opinion the links are not well formed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.