Help to populate an array with objects using a while

Asked

Viewed 1,050 times

1

I want to read lines from an SQL and use them to create objects and finally put these objects in an array. But at the end my methods (from the Object class) do not print the object name.
The code to read only one line works, so the my problem certainly is in the array.

<?php
    require_once 'Object.php';
    require("connect.inc");
    connect_db() or die ("Error.");
    $result=mysql_query("SELECT * FROM object") or die ("Error");

    $count = 0;
    $c[] = array();
    while ($line=mysql_fetch_array($result) && $count < 6) {
        $count ++;
        $c[$count] = new Object($line);
    }

    print ($c[1]->getName()); // Example

?>
  • 1

    Hi Bernardo, can you translate the question to English?

  • $Count should be $cont or vice versa. And snap the brackets from the $c statement.

  • sorry, actually this was a transcription error, both are equal

  • The error is in if. It is $cont < 6, when, I believe, I should be $count < 6

  • sorry, as I translated the code to post, I ended up missing this variable at the time of transcription, but all "Count" are equal in the code

  • What is the use of this counter? When there are no more lines, the loop will be finished, there is no need for counter.

  • I’m not familiar with PHP, I thought I needed one like in Java. Alias, this inside the vector, but it is used pq I want only the first 6 rows of the table.

  • 1

    What is this Object class? You created it?

Show 3 more comments

1 answer

5


If the intention is to store the rows in the form of an object in the array and display later, it could be done so:

<?php
    require_once 'Object.php';
    require("connect.inc");
    connect_db() or die ("Error.");
    $result=mysql_query("SELECT * FROM object") or die ("Error");


    $c[] = array();

    while ($line=mysql_fetch_object($result) ) {
        $c[] = $line;
    }


    foreach($c as $line){
        echo $line->coluna1;//altere pelo nome das colunas da sua tabela
    }

?>

I strongly suggest you remove the functions mysql, for its development has been discontinued. Replace with functions mysqli or PDO.

  • The intention is to create objects with these lines and store them in the array. The Object constructor uses the "$line" vector to create the object.

  • @Bernardoaraujo php already does this, test this code I posted to see. To see that you’re coming from java, php is more flexible in many things.

  • But where are the objects being created? I did not find the call to the builder.

  • @Bernardoaraujo have you tested the code? The function mysql_fetch_object transforms the resgadas lines of the table into objects, where the properties are the columns of the table.

  • I already have a specific class with methods I want to use to create these objects.

  • @Bernardoaraujo then it would be interesting if you edit the question and show the structure of the select table, and also add this class so that it is possible to test.

  • Thanks for all the help, I used this answer as a basis to make a solution that served me.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.