![]() |
Two Queries, one html table - 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: Two Queries, one html table (/showthread.php?tid=6790) |
Two Queries, one html table - El Forum - 03-11-2008 [eluser]Kemik[/eluser] Hello, I've got two queries, one to get latest 5 news and one to get latest 5 comments. I'm going to display them in a table on my homepage. $query = news $query2 = comments Code: <table width="95%"> Usually I'd use <?php foreach($query->result() as $row): ?> content <?php endforeach; ?> but that won't work because the comments are pulled in one query and the news is pulled in a second query. Anyone got any ideas? Two Queries, one html table - El Forum - 03-11-2008 [eluser]xwero[/eluser] As they both are joined with the news id you can merge them in the controller. Code: $comments = $query2->result_array(); Two Queries, one html table - El Forum - 03-11-2008 [eluser]Kemik[/eluser] The data isn't related otherwise I would have used a join. Two Queries, one html table - El Forum - 03-12-2008 [eluser]kgill[/eluser] I think I see what you're trying to do, two columns of unrelated data sitting next to each other in a table. Is there a specific reason why you need to use a table instead of something simpler like outputting the results to separate divs placed next to each other? If the table has to be there your best bet is probably nested tables, failing that you could do some really ugly coding and ditch the associative arrays in favor of plain 0 based ones - that way you can use the index of the one result set you're looping through to output the results of the other. Of course that brings a host of other code needed, count each result and see if one is bigger, use the larger one in your foreach, add conditional logic to make sure the index exists in the other before you attempt to reference it, etc. - K Two Queries, one html table - El Forum - 03-12-2008 [eluser]xwero[/eluser] I have to agree with kgill mixing unrelated data can create a mess of checks and you have to use an array to accomplish it instead of an object. But it can be done Code: $comments = $query2->result_array(); Two Queries, one html table - El Forum - 03-12-2008 [eluser]webthink[/eluser] There's about 5 different ways to do what you want to do but if you're sure you'll always have an equal number of news and comments this is probably the easiest. You just advance the pointer in query two at the same time you do in query 1 only you do it as a lone statement rather than a foreach param. Code: <?php foreach($query->result() as $row): ?> Two Queries, one html table - El Forum - 03-12-2008 [eluser]xwero[/eluser] The problem with my previous snippet is that the comments and news columns can change and that is not what you want Code: $comments = $query2->result_array(); ![]() Two Queries, one html table - El Forum - 03-12-2008 [eluser]Kemik[/eluser] It's basically a table showing the latest 5 news articles and latest 5 comments. Comments can be for different news articles. I'm using a basic select statement with a 5 row limit ordered by date_posted DESC. I'll try the code xwero posted above this reply and if all else fails I'll just use a div and restlye it to look like the table. |