-2

How can i get row href to be 'id' string value from database ? In other words, how can i show multiple arrays in one foreach ?

If theres any other logical errors in code you can point that out too.

<?php 

$query = mysql_query("SELECT `id`, `url` FROM `bse` ORDER BY `id` DESC LIMIT 9"); 
while ($row = mysql_fetch_array ($query))

$post_id[] = $row['id'];
$rid[] = $row['url'];

}

$array = array(
    "pid" => $post_id,
    "vid" => $rid,
);

foreach (array_chunk($array["vid"], 3) as $rida) { ?>
    <tr>
    <?php foreach ($rida as $array["vid"])
                {  ?>
<td><div class="a45"><a href="<?php echo $array["pid"]; ?>" title="nim" ><?php 
echo $array["vid"];

?>
</a></div></td>
    <?php } ?>
    </tr>
<?php } ?>
</table>
cmd
  • 11,622
  • 7
  • 51
  • 61
  • Just a quick note, you don't need to put `id` `url` `bse` inside backticks, since they are not [SQL reserved words](http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html). – Funk Forty Niner Oct 02 '13 at 19:58
  • The MySQL extension is outdated. Please don't use it anymore! MySQLi (with prepared statements) and PDO are good alternatives. – ComFreek Oct 02 '13 at 19:59
  • @Fred-ii-, i think the motive of the backticks is for when the names of the columns, tables, etc, have problematic characters. – Rafael Barros Oct 02 '13 at 20:07
  • 1
    @RafaelBarros You think? You may be right, a lot of things I'm not aware about SQL (learning DB for only a month now) and will keep that in mind, thanks. *"Stranger things are meant to happen."* – Funk Forty Niner Oct 02 '13 at 20:08
  • What are you trying to do with the code? – bestprogrammerintheworld Oct 02 '13 at 20:10
  • @Fred-ii-, yes, just think, i could search to be certain, but, you know... laziness. One more thing: i think :) the backticks it's used just is MySQL. Others dbs, others characters. – Rafael Barros Oct 02 '13 at 20:15
  • 1
    @RafaelBarros Well I certainly won't leave it out of the equation. *"I'll just take your word for it"* ;-) – Funk Forty Niner Oct 02 '13 at 20:17

2 Answers2

1

You have two options. One is merging the arrays to just one array, take a read here:

how to join two multidimensional arrays in php

PHP combine two associative arrays into one array

The other (Thanks to Rafael!) is much easier.. Use something like this:

foreach($array as $i=>$v){
    $other_array[$i];
}

You can use the $i and look in the other array. Credits ofcourse to Rafael.

Community
  • 1
  • 1
Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
0

Is this what you're looking to do?

<?php 
$query = mysql_query("SELECT `id`, `url` FROM `bse` ORDER BY `id` DESC LIMIT 9");

if($query){
    ?>
    <table>
    <?PHP
        while ($row = mysql_fetch_array ($query)){
             ?>
          <tr>
            <td>
                <div class="a45">
                    <a href="<?php echo $row['id']; ?>" title="nim" >
                        <?php echo $row['url']; ?>
                    </a>
                </div>
            </td>
          </tr>
          <?php
        }
    ?>
    </table>
    <?PHP
} else {
    echo "No results";  
}
?>

If not, what is it that you're trying to accomplish?

Andre
  • 603
  • 7
  • 19