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'
)
);
Something like :
$listofimages = array();
foreach ( $rowset as $row ) {
$listofimages[] = array(
'titre' => $row['titre'],
'texte' => $row['texte'],
'image' => $row['image']
);
}
?
foreach ($listofimages as $image) {
echo '<img src="'.$image['image'].'" />';
}
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>";
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.
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);
$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;
}
}