CodeIgniter Forums
Display MySQL rows - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Display MySQL rows (/showthread.php?tid=27828)



Display MySQL rows - El Forum - 02-22-2010

[eluser]Assim[/eluser]
This could be the most simplest questions any one can ask but when I do, the rows from the MySQL table are not shown but the word "Array" is shown instead. It works when I use the parser, but when I write it in PHP tags in the view it doesn't.

I don't want to make it a long, but here's what I wanted to do. I have a table called links with a column named URL, how do I display the URL in rows in the view.

Here's the query that I would use:
SELECT * FROM links

Just a simple example of how to use might solve this problem.


Display MySQL rows - El Forum - 02-22-2010

[eluser]rogierb[/eluser]
Ae you refering to looping through a result set?
Code:
foreach($resul_set->result() as $row )
{
echo $row->field;
}

Can you show us the code?


Display MySQL rows - El Forum - 02-22-2010

[eluser]Assim[/eluser]
[quote author="rogierb" date="1266850050"]Ae you refering to looping through a result set?
Code:
foreach($resul_set->result() as $row )
{
echo $row->field;
}

Can you show us the code?[/quote]
Here's my code.

Controller:
Code:
$PageReferences = $this->db->query("SELECT URL FROM pagereferences WHERE PageID = '$PageID'");
$data['References'] = $PageReferences->result_array();
$this->load->view('references', $data);

This is what I wrote in the view:
Code:
<?php foreach($References as $URL): ?>
&lt;?php echo $URL; ?&gt;<br />
&lt;?php endforeach; ?&gt;

The value of $URL is "Array" when echoed.


Display MySQL rows - El Forum - 02-22-2010

[eluser]rogierb[/eluser]
Try: $URL['URL'];


Display MySQL rows - El Forum - 02-22-2010

[eluser]Assim[/eluser]
[quote author="rogierb" date="1266850732"]Try: $URL['URL'];[/quote]
Still doesn't work.

I also tried &lt;?php foreach($References['URL'] as $URL): ?&gt; but I got a unidentified index: URL error.


Display MySQL rows - El Forum - 02-22-2010

[eluser]rogierb[/eluser]
Try

Code:
&lt;?php print_r($References); ?&gt;<br />
&lt;?php foreach($References as $URL): ?&gt;
&lt;?php print_r($URL); ?&gt;<br />
&lt;?php echo $URL; ?&gt;<br />
&lt;?php endforeach; ?&gt;

To see what the array is composed of.

It might be the array is empty.


Display MySQL rows - El Forum - 02-22-2010

[eluser]Assim[/eluser]
[quote author="rogierb" date="1266852028"]Try

Code:
&lt;?php print_r($References); ?&gt;<br />
&lt;?php foreach($References as $URL): ?&gt;
&lt;?php print_r($URL); ?&gt;<br />
&lt;?php echo $URL; ?&gt;<br />
&lt;?php endforeach; ?&gt;

To see what the array is composed of.

It might be the array is empty.[/quote]
This is what I got:
Code:
Array ( [0] => Array ( [URL] => http://assim.me ) [1] => Array ( [URL] => http://destinationoman.com ) )
Array ( [URL] => http://assim.me )
Array
Array ( [URL] => http://destinationoman.com )
Array

Those are what in my DB. So how can I display them?


Display MySQL rows - El Forum - 02-22-2010

[eluser]rogierb[/eluser]
Based on the array I would say:

[code]
&lt;?php foreach($References as $array=>$row): ?&gt;
&lt;?php echo $row['URL']; ?&gt;<br />
&lt;?php endforeach; ?&gt;


Display MySQL rows - El Forum - 02-22-2010

[eluser]Assim[/eluser]
Wow, thanks! It worked. Smile I have been trying to make this work for days, and thanks for helping me.