tags:

views:

42

answers:

6

How can i make an array like below through a loop? The text will generated from database!

 $listofimages = array(
        array(  'titre' => 'PHP',
                'texte' => 'PHP (sigle de PHP: Hypertext Preprocessor), est un langage de scripts (...)',
                'image' => './images/php.gif'
        ),
        array(  'titre' => 'MySQL',
                'texte' => 'MySQL est un système de gestion de base de données (SGDB). Selon le (...)',
                'image' => './images/mysql.gif'
        ),
        array(  'titre' => 'Apache',
                'texte' => 'Apache HTTP Server, souvent appelé Apache, est un logiciel de serveur (...)',
                'image' => './images/apache.gif'
        )
    );
A: 
foreach ($listofimages as $image) {
    echo '<img src="'.$image['image'].'" />';
}
Sjoerd
this does not even answer OP's question
Starx
It seems I misinterpreted the question as "How can i make an array like below go through a loop?"
Sjoerd
+1  A: 

do something like this,

$result = mysql_query("SELECT * FROM table;");
while($row = mysql_fetch_assoc($result) {
    //$row will hold all the fields's value in an array
    $mymainarray[] = $row; //hold your array into another array
}

//To display
echo "<pre>";
print_r($mymainarray);
echo "</pre>";
Starx
A: 

If you only select the fields titre, texte and image in your query, then it is just (assuming MySQL):

$listofimages = array();

$result = mysql_query("SELECT titre, texte, image FROM table");

while((row = mysql_fetch_assoc($result))) {
    $listofimages[] = $row;
}

mysql_fetch_assoc() will fetch the results in an associative array.

Felix Kling
So my question is also downvoted because it is very similar to others? That is ridiculous...
Felix Kling
A: 

Something like :

$listofimages = array();

foreach ( $rowset as $row ) {

    $listofimages[] = array(
        'titre' => $row['titre'],
        'texte' => $row['texte'],
        'image' => $row['image']
    );

}

?

hsz
A: 

Let's say you have done a query and get a result identifier:

$result = mysql_query("SELECT * FROM table;");

Now you can get every row as an array by using mysql_fetch_assoc($result). Now just make a final array and add all these arrays to it:

$final_array = array();
while($row = mysql_fetch_assoc($result) !== false) {
    $final_array[] = $row;
}

print_r($final_array);
Peter Smit
this is an identical copy of my answer except for the use of mysql_fetch_assoc($result);
Starx
@Starx: Your answer is wrong (or at least not 100% correct ;)). If you use `mysql_fetch_array`, then you get the rows as associative **and** numerical array. Therefore, every column is contained twice in the array.
Felix Kling
+1 to compensate downvote.
Felix Kling
@Felix Kling, thanks for the correction. I will update it right away
Starx
I was already almost finished writing it when other answers came. Does it make my answer not useful? I don't think so.
Peter Smit
But yeah, they are *very* similar
Peter Smit
A: 
$mult_array = array();

$result = mysql_query("SELECT col1,col2,... FROM table");

if(mysql_num_rows($result)>0){
  while((row = mysql_fetch_assoc($result)) {
      $mult_array[] = $row;
  }
}

// if u want to pick some specific cols from data selected from database then- 



if(mysql_num_rows($result)>0){
$temp_arr = array();
  while((row = mysql_fetch_assoc($result)) {
      $temp_arr['col1'] = $row['col1'];
      $temp_arr['col2'] = $row['col2'];
      //... so on
      $mult_array[] = $temp_arr;
  }
}
Sadat