tags:

views:

71

answers:

6

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>
A: 
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.

Tokk
I dont want to use it in the same while loop.
ddb
$post_id[] is array. $post_id - is not array.
Alexander.Plutov
+2  A: 

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>';
}
codaddict
All your lines will be the same. Where $i?
Alexander.Plutov
sizeof() more faster than count()
Alexander.Plutov
@Alexander you are terrible wrong.
Col. Shrapnel
Check this with firebug. Or you can read about operations speed on http://phpbench.com/
Alexander.Plutov
@Alexander fireWHAT? Are you REALLY think that little bit of server CPU time be even noticed from the client side? I'm afraid you do not understand what are you talking about. And don't tell me of that site. phpbench is just a piece of shit.
Col. Shrapnel
Ok, boss. A piece of...
Alexander.Plutov
@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.
Col. Shrapnel
@Alexander Shrapnel is right, but to put it in less aggressive terms: [`sizeof`: Description: "This function is an alias of: `count()`."](http://php.net/sizeof) Please provide irrefutable proof that an alias of a function is faster than the function itself. Thank you. :)
deceze
A: 
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>';
}
Alexander.Plutov
+1  A: 
<?
$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 ?>
Col. Shrapnel
A: 

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'];
    ...
}
deceze
A: 

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.

makki