Question

I have a question: I make a pre for news:

Array
(
[0] => Array
    (
        [name] => tag
        [id] => 57
        [title] => Article1,Article2,Article3
        [views] => 53,54,58
        [smallimage] => Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg
        [date] => 2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40
    )

) How to create the foreach in view to show 1 title 1 views and 1 date...I create an foreach but show first all titles,all views,all dates; My foreach:

<?php if ($news):?>

        <?php foreach($news as $n):?>
            <p>Tag:<?php echo $n['name'] ?></p>

            <div class="container">

                        <img src="<?php echo config_item('content_folder');?>news/small/<?php echo $n['smallimage']; ?>" alt="">
                        <?php echo $n['title'] ?><br/>
                                                    <?php echo $n['views'] ?>



        <?php endforeach ?>
    <?php endif; ?>

With this foreach I get: Article1,Article2,Article3 53,54,58...I want to get Article1 53,Article2 54, Article3 58....Help me please

Was it helpful?

Solution

Try this:

<?php
$news  =   array(array(  "tag",
         57,
         "Article1,Article2,Article3",
         "53,54,58",
          "Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg",
        "2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40"
    ) );

?>

<?php  if ($news){?>
<p>Tag:<?php echo $news[0][0]; ?></p>
 <div class="container">
     <img src="<?php echo config_item('content_folder');?>news/small/<?php echo $news[0][4]; ?>" alt="">
      <?php $views = explode(',',$news[0][3]);?>
      <?php $article = explode(',',$news[0][2]);?>
      <?php for($i=0;$i<count($article);$i++){
            echo $article[$i].'-'.$views[$i];
     } ?>
</div>
 <?php   }?>

I don't know where is "config_item('content_folder')" function result.Try this. And let me know whether it is works or not. Feel free to ask help.

OTHER TIPS

    <?php foreach($news as $n):?>
        <?php $titles = explode(",", $n['title']); ?>
        <?php $smallimages = explode(",", $n['smallimage']); ?>
        <?php $views= explode(",", $n['views']); ?>
        <p>Tag:<?php echo $n['name'] ?></p>

        <div class="container">

                    <img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[0]; ?>" alt="">
                    <?php echo title[0] ?><br/>
                    <?php echo views[0] ?>
                    <img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[1]; ?>" alt="">
                    <?php echo title[1] ?><br/>
                    <?php echo views[1] ?>
                    <img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[2]; ?>" alt="">
                    <?php echo title[2] ?><br/>
                    <?php echo views[2] ?>


    <?php endforeach ?>
<?php endif; ?>

I am assuming that for example 'Article1,Article2,Article3' is a string, so you would have to use:

$n['title'] = explode(',',$n['title']) 

To make it into an array. Same goes for views, smallimage and date. Here's an example on how to apply that:

<p>Tag:<?php echo $n['name'] ?></p>
<div id="container">
<?php
$n['title'] = explode(',',$n['title']) 
$n['views'] = explode(',',$n['views']) 
$n['smallimage'] = explode(',',$n['smallimage']) 
foreach ($n['title'] as $key=>$value) {
 echo "<img src='" . config_item('content_folder') . "news/small" . $n['smallimage'][$key] . "' alt='' />";
 echo $n['title'][$key] . "<br />";
 echo $n['views'][$key]
 }
?>
</div>

You would place this in the existing foreach-loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top