displaying multiple results in a view |
[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() { Code: function home() and view is Code: <tr> now this displays only the first row of table...how do i display all the rows with their attribute?
[eluser]TheFuzzy0ne[/eluser]
Code: <?php foreach($events as $event): ?> Your HTML is not valid. A <br /> 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.
[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.
[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.
[eluser]junaids[/eluser]
thanks for help. but in the view only first row from the table is repeated over and over.......
[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
[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() It may also be wise to have your model check for a result first, and return FALSE if there isn't one.
[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.
[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: <?php while($event = mysql_fetch_row($events)):?>
[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() { and the controller is Code: function home() and view is what u have posted few mins ago. sorry to be so irritating but cant get out of this on my own. |
Welcome Guest, Not a member yet? Register Sign In |