Reputation: 7
I am trying to convert my php array into html using php implode.
This is my code:
$myarray = array();
while ($row = mysql_fetch_assoc($result)) {
$myarray[] = array("title"=>$row['title'],
"name"=>$row['title'],
"content"=>$row['content'],
"image" =>
array(
"cls"=>"slide-image",
"_src"=>$row['src'],
"source"=>$row['source']
)
);
}
and
$rows = array_fill( 0, count( $myarray[0] ), "" );
$keys = array_keys( $myarray[0] );
foreach( $myarray as $k => $record )
for( $i=0, $max=count( $rows ); $i < $max; $i++ )
$rows[ $i ] .= $record[ $keys[ $i ] ];
print implode( "", $rows );
The output is
title 1, title-2, content for title 1, content for title 2ArrayArray
I want as
title 1, content for title 1, title 2, content for title 2
and i don't know why the Array is coming. Any help please ?
Upvotes: 0
Views: 156
Reputation: 4449
Given the following sample array from your database:
$data = array(
array(
'title' => 'Title 1',
'name' => 'Title 1',
'content' => 'Content 1',
'image' => array(
'src' => 'a',
'title' => 'b',
'alt' => 'c'
)
),
array(
'title' => 'Title 2',
'name' => 'Title 2',
'content' => 'Content 2',
'image' => array(
'src' => 'a',
)
)
);
The following code will loop through it:
$rows = array();
foreach($data as $row) {
$img = '<img ';
foreach($row['image'] as $attr => $value) {
$img .= $attr . '="' . $value . '" ';
}
$img .= '/>'; //Close $img
$rows[] = $row['title'] . ', ' . $row['content'] . ', ' . $img;
}
print implode(', ', $rows);
Producing:
Title 1, Content 1, <img src="a" title="b" alt="c" />, Title 2, Content 2, <img src="a" />
UPDATE
You could do this while pulling the data from your database:
$rows = array();
while($row = mysql_fetch_assoc($result)) {
$img = '<img class="' . $row['cls'] . '" src="' . $row['src'] . '" title="' . $row['source'] . '" />';
$rows[] = $row['title'] . ', ' . $row['content'] . ', ' . $img;
}
print implode(', ', $rows);
Upvotes: 1
Reputation: 11622
Update because of OP's comment that they want images too.
You can just tweak the example I already gave you just a bit to get images too. Just place this bit of code right after you fill $myarray
with values from the database. I also added HTML tags to make my example more straight forward for you.
$html = '';
foreach($myarray as $record){
if($html)
$html .= '<hr/>';
$html .= '<h1>' . $record['title'] . '</h1>';
$html .= '<p>' . $record['content'] . '</p>';
$image = $record['image'];
$html .= '<img class="' . $image['cls'] . '" src="' . $image['_src'] . '" title="' . $image['source'] . '"/>';
}
Upvotes: 0
Reputation: 741
Trying to express an array as a string will produce the string Array
. In your case it's the two $record['image'] fields. Try adding this
$rows = array_fill( 0, count( $myarray[0] ), "" );
$keys = array_keys( $myarray[0] );
foreach( $myarray as $k => $record )
for( $i=0, $max=count( $rows ); $i < $max; $i++ ){
if (is_array($record[$keys[$i]])){
$rows[$i].=implode("",$record[$keys[$i]]);
}
else $rows[ $i ] .= $record[ $keys[ $i ] ];
}
print implode( "", $rows );
You can make the specific handling for subarrays more complicated, but that's an example.
Upvotes: 0
Reputation: 612
Assuming that you are fetching these results from a DB, I offer this solution:
$myData = array();
while ($row = mysql_fetch_array($result)) {
$myData[]['title'] = "<h2>".$row['title']."</h2>";
$myData[]['content'] = "<p>".$row['content']."</p>";
$myData[]['imageData'] = array("<img src='".$row['src']."' class='slide-image' />",$row['source']);
}
From here, you can iterate through $myData using a foreach loop.
Upvotes: 0