0

Hello I just started learning PHP about 8 days ago. I am trying to load comments form an SQL table and put them in order by their ID. To do this I want to use an array and add to the index that the comment id is then it would be in order.

EX: (Ik this is not proper code)

while loop through SQL table{
   array[3] = This is comment 3.
   array[1] = This is comment 1.
   array[0] = This is comment 0.
   array[2] = This is comment 2.
}

As you can see it puts the comments in correct order just by their index. How would I do this in PHP?

Here is what I have so far and it does NOT work:

$return = "";
    $array = array();
    $lowers = 0;


    $res2 = mysql_query("SELECT * from `".Mod::$id."_comments`");
    if($res2){
        while($row = mysql_fetch_assoc($res2)){
            if($row['id'] < $lowers){
                $lowers = $row['id'];
            }
            $name = $row['id'] . "_delete";
            $array[$row['id']] = "<div class=\"caption\"><hr><h5>"
            . getUserByUUID($row['uuid']) . " (" .  date('m/d/Y', $row['timestamp']) . ") <input type=\"button\" name=\"" . $name .
            "\" id=\"" . $name . "\" value=\"Delete\" onClick=\"CALLJAVASCRIPTFUNCTIONHERE()\" ></h5>
                    <figure class=\"img-polaroid\">" . 
                    $row['comment'] . "</figure>
                </div>";//TODO CHANGE THE CALL JAVA SCRIPT FUNCTION TO THE PROPER FUNCTION!!!!!
            alert("ARRAY: " . $array[$row['id']]);//This is getting called but it does nothing.  Also I made a php function called alert that DOES work.
        }
    }



    for($loop = $lowers; $loop < (count($array) + $lowers); $loop++){
        alert("LOOP: " . $loop);
        $return = $return + $array[$loop];
    }

    return $return;

    for($loop = $lowers; $loop < (count($array) + $lowers); $loop++){
        alert("LOOP: " . $loop);
        $return = $return + $array[$loop];
    }

    return $return;

Thank you for any help.

1
  • why you want to use array for this this can be done by simple query Commented Oct 4, 2014 at 3:22

3 Answers 3

1

Buddy your approach is totally wrong. Why looping and processing unnecessarily?

You could do that at the time of executing query by using ORDER BY Clause.

Sign up to request clarification or add additional context in comments.

Comments

0

If you're looking to sort the PHP array by index (key), then what you're looking for is a built in function called ksort().

From PHP.net (the link above): Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.


If you're looking to sort the returned ID's from the database, just change your query to:

"SELECT * from `".Mod::$id."_comments` ORDER BY `id` ASC" (DESC if you want the newest first)

Comments

0

You dont need that whole code, remove that and try the code in this answer , and change the value where you have to, dont forget to change YourTableName to your actual table name. what it gonna do its going to select every thing and will order result by their id. Thanks,

$res2 = mysql_query("SELECT * from YourTableName ORDER BY id ASC");
//Change YourTableName to your actual table name
  if($res2){
        while($row = mysql_fetch_assoc($res2)){

            echo $row['id'].'  :'.$row['comment'].'<br/>';

        }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.