Welcome Guest, Not a member yet? Register   Sign In
displaying multiple results in a view
#1

[eluser]junaids[/eluser]
hi
i want to retrieve DB table that has multiple rows and pass it to a view.
my model is
Code:
function getEvent() {
    
    $result = mysql_query("SELECT * FROM event");
        $row = mysql_fetch_array($result);
       return $row;
     }
controller is
Code:
function home()
     {
          $data['events'] = $this->eventsmodel->getEvent();
        $this->load->view('events', $data);}

and view is
Code:
<tr>
<td height="19" align="left" valign="top" class="label2">Event name:&lt;?php echo $events['Event_name']; ?&gt;<br/></tr>

<tr>
<td height="19" align="left" valign="top" class="label2">Web link:&lt;?php echo $events['Event_weblink']; ?&gt;<br/></tr>

now this displays only the first row of table...how do i display all the rows with their attribute?
#2

[eluser]TheFuzzy0ne[/eluser]
Code:
&lt;?php foreach($events as $event): ?&gt;
<tr>
    <td height="19" align="left" valign="top" class="label2">
        Event name:&lt;?php echo $event['Event_name']; ?&gt;
    </td>
</tr>
<tr>
    <td height="19" align="left" valign="top" class="label2">
        Web link:&lt;?php echo $event['Event_weblink']; ?&gt;
    </td>
</tr>
&lt;?php endforeach; ?&gt;

Your HTML is not valid. A &lt;br /&gt; can't appear within a table row using any doctype I know of. Also, I'm not quite sure why you're using a table for a single column of data.
#3

[eluser]xwero[/eluser]
If you look at the php.net mysql_fetch_array function examples you see the function is called in a while loop.
This means the function just gets connected to the result, returns the current row from its internal counter and outputs the row in the format defined by the function.

To return the whole result you are going to have to add the loop.
#4

[eluser]TheFuzzy0ne[/eluser]
You'll also want to use mysql_fetch_assoc() instead.

I'd highly recommend you check out the active record class. It can simplify basic queries for you.
#5

[eluser]junaids[/eluser]
thanks for help.
but in the view only first row from the table is repeated over and over.......
#6

[eluser]Dam1an[/eluser]
The reason it repeats the same row again and again, is that you never increment the internal position in the results

Try with this in your view
Code:
// Obviously wrap the table structure around this
while($event = mysql_fetch_row($events)) {
    echo $event['event_name'];
    echo $event['event_weblink'];
}
#7

[eluser]TheFuzzy0ne[/eluser]
Yeah, sorry about that. It hadn't occurred to me that you weren't getting the entire array. I'm too used to the CodeIgniter Result object.

Code:
function getEvent()
{
        $result = mysql_query("SELECT * FROM event");
        foreach ($row = mysql_fetch_assoc($result))
        {
            $arr[] = $row;
        }

        return $arr;
    }

It may also be wise to have your model check for a result first, and return FALSE if there isn't one.
#8

[eluser]junaids[/eluser]
@ thefuzzyone. thr is a problem with the forech loop u wrote.
it give an error.
Parse error: syntax error, unexpected ')'
i think we cant pass the $value argument to the forech loop as we have
Code:
mysql_fetch_assoc($result)


@dam1an
i did not undersatnd the while loop u wrote
if u can plz write it like thefuzzyone wrote foreach loop in the first reply to this thread.
#9

[eluser]Dam1an[/eluser]
The reason I used a while loop, was because I want it to execute until the fecth_row returns false
A for (or foreach) loop is used to iterate over a collection (eg an array) of items
I'm not sure if you can (as I never have) use a for/foreach loop to get each row, but a drop in replacement of fuzzy's code would be
Code:
&lt;?php while($event = mysql_fetch_row($events)):?&gt;
    <tr>
        <td height="19" align="left" valign="top" class="label2">
            Event name:&lt;?php echo $event['Event_name']; ?&gt;
        </td>
    </tr>
    <tr>
        <td height="19" align="left" valign="top" class="label2">
            Web link:&lt;?php echo $event['Event_weblink']; ?&gt;
        </td>
    </tr>
&lt;?php endwhile;?&gt;
#10

[eluser]junaids[/eluser]
but in the model i have a mysql_fetch_assoc call.
so its not working with one mysql_fetch_assoc call in model and one mysql_fetch_assoc in the view.
my model is
Code:
function getEvent() {
    
    $result = mysql_query("SELECT * FROM event");
        $row = mysql_fetch_assoc($result);
       return $row;
     }

and the controller is
Code:
function home()
     {
              $data['events'] = $this->eventsmodel->getEvent();
            $this->load->view('events', $data);
    }

and view is what u have posted few mins ago. sorry to be so irritating but cant get out of this on my own.




Theme © iAndrew 2016 - Forum software by © MyBB