0

Problem: Can only output one field from each array

 <?php
    error_reporting(0);
    session_start();
    include('connect.php');
    ob_start();

    $category = array(
        "dates" => array(),
        "paragraphs" => array(),
        "titles" => array()
    );

    if ($result = $mysqli->query("SELECT * FROM tips")){
        if($result->num_rows){
            while($row = $result->fetch_array(MYSQLI_ASSOC)){
            //printf ("%s %s %s\n",$row["date"], $row["title"], $row["paragraph"]); 
            $dates= array($row["date"]);
            $titles = array($row["title"]);
            $paragraphs = array($row["paragraph"]);
            }
            }
            $result->free();
        }

    ?>

I want the last in the array to be displayed first so I'm using this

<?php echo $titles[count($titles)-1]?>

I think the problem is with my loop, I think its reassigning the array each time it loops round

5
  • 3
    Each iteration of while creates a new array in your vars, which first position stores current row data iteration. It overwrites the last one each time, try not $data = array($row['data']) but $data[] = $row['data']. This way you push the values of each iteration into a new index of the array. Commented Oct 7, 2015 at 19:47
  • 1
    Works perfectly Alan, thank you very much. Write it as the answer and ill accept Commented Oct 7, 2015 at 19:49
  • Your code assumes that$titles is an array, when it is not. Commented Oct 7, 2015 at 19:50
  • Do you really need to SELECT all items from your table? why not just get the last one you need? Commented Oct 7, 2015 at 20:03
  • I don't need to select all indeed, I shall use LIMIT in my select statement Commented Oct 7, 2015 at 20:05

2 Answers 2

3

Each iteration of while creates a new array in your vars, which first position stores current row data iteration.

It overwrites the last one each time, try not $data = array($row['data']) but $data[] = $row['data'].

This way you push the values of each iteration into a new index of the array.

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

Comments

1

you are not adding values to the $category array properly :

while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
   $category['dates'][] = $row["date"];
   $category['titles'][] = $row["title"];
   $category['paragraphs'][] = $row["paragraph"];
}

as it is now, with $dates= array($row["date"]) etc, you just assign the values to the same (never declared) $dates variable over and over.

2 Comments

yes and no.. this will create arrays for each column set rather than per each row set.
@CodeGodie, $category['titles'] is an array, $category['titles'][] = $row["title"] will add new elements to that array; by that OP can display the last item with echo $category['titles'][count($category['titles'])-1], I guess that he have forgotten his $category declaration too :)

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.