0

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 ?

4
  • What exactly are you trying to do? There has to be a more simplified solution to this. Commented Dec 4, 2012 at 17:27
  • I just want to get my array data as html using php. That's all.. If you know any simplified solution, please tell me. Commented Dec 4, 2012 at 17:28
  • I would start here php.net/manual/en/language.types.array.php Commented Dec 4, 2012 at 17:29
  • @user1868475 Are you trying to get the image to also be in the string that is displayed or do you only want the title and content? Commented Dec 4, 2012 at 17:35

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

is this possible to use this loop with my array ?
@user1868475 Yes. Your array contains a title, content, and image. Just change $data to whatever you have called your array.
@user1868475 Look at my update that will allow you to pull the information right out of the database, instead of building the data array, and then looping through it again.
@Aust Thanks. I tried something like that in my original answer, but then had to take it out because the OP's DB structure was different than the data array. Fixed it now. :)
0

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.

Comments

0

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.

2 Comments

Undefined variable: rows in 47,53 lines
I meant for that code to just replace the relevant section in your original code. Added the definition of rows and keys above, you'll still need your first block of code as well.
0

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'] . '"/>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.