Welcome Guest, Not a member yet? Register   Sign In
Retrieve data from database problem
#1

[eluser]Dcahrakos[/eluser]
I really didnt know what to name the thread, but thats about as close as I can come to describing it....

What im trying to do is this,

I have a table in a database called reviews and it has a few fields, one of them being game, and on my page what im trying to do is use a foreach loop to print out each letter in the alphabet from a-z, and if there is a review in the table that begins with that letter it displays it below the letter, for example

A
(horizontal ruler here)
Game name that starts with A
Another game that starts with A

and so on all through the alphabet.

does anyone know the best way to go about this? I just started using Code Igniter, and im fairly good with PHP, but for some reason the answer to this escapes me.

Thanks in advance.
#2

[eluser]Armchair Samurai[/eluser]
I don't know if this is the best way, but once you retrieve the values from your database (ORDER BY game ASC, of course), just loop through the array.
Code:
$game_list = reset($array_from_database);

foreach (str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ') as $val)
{
    $this_game = current($game_list);

    echo $val.'<hr />';

    while (strtoupper(substr($this_game->game, 0, 1)) == $val)
    {
        echo $this_game->game.'<br />';

        $this_game = next($game_list);
    }
}
Of course, the presentation would need to be cleaned up but you get the basic idea, I hope. You might also want to add numbers to the mix if necessary.
#3

[eluser]Dcahrakos[/eluser]
Hi,

Thanks for the reply. I tried the code you posted, replaced the $array_from_database with my variable that returns an array from the reviews table in the database, but im getting the following error:

Quote:Message: Trying to get property of non-object

Filename: views/review_page.php

Line Number: 21

and line 21 is:
Code:
while (strtoupper(substr($this_game->game, 0, 1)) == $val)

this is what im using in my model to get the stuff from the database
Code:
function get_review_list()
    {
    $query = $this->db->query('SELECT * FROM reviews ORDER by game ASC');

    return $query->result_array();

    }

Thanks.
#4

[eluser]Armchair Samurai[/eluser]
You're returning a two dimensional array, not an array of objects, so you'll need to change all the object examples into arrays (e.g. $this_game->game to $this_game['game'])
#5

[eluser]Dcahrakos[/eluser]
Thanks, it works perfectly. Smile




Theme © iAndrew 2016 - Forum software by © MyBB