0

new to php, I have read dozens of 'insert data into arrays' posts on the net, but none seem to describe my exact problem, (or I am too ignorant to recognize it) So here we go.

I get some data from a SQL database. I want to present this in a table. I have a working script that does this, but it has a slight malfunction, it doesn't show any empty results (rows) in the table. To get the desired result I my attempt 2.0 starts now with an empty table, with all the rows, and fill them in with data later.

$names = array('Alice', 'Bob', 'Charles', 'Dick', 'Emy');
for ($i=0; $i< count($names); $i++) 
 {
    $empty_table[] = array("Name" => $names[$i], "Total" => 0, "Wins" => 0, "Losses" => 0) ;
 }

This gives me an array that I can display as a table, with 5 data rows, each containing a value for the field with a Name key (Alice to Emy) and otherwise filled with values set to 0 for all other keys. It looks like:

Name Total Wins Losses

Alice 0 0 0

Bob 0 0 0

Charles 0 0 0

Dick 0 0 0

Emy 0 0 0

From the database I want to fill in the values that are 0, IF and only IF there is something in the database. I get the data with

for ($i=0; $i< count($names); $i++) {
$sql = "SELECT
        SEVERAL THINGS    
        WHERE Something = '".$names[$i]."'";
    $result = mysqli_query($conn,$sql);
// Not the real query obviously, but that part works and gives the intended result.
    while ( $row = mysqli_fetch_assoc($result) ) {
    Do something with the result in $row;
    }   // end while
}       // end for

$row is an array with the same structure as one of the values of $empty_table. Example:$row could be: Array ( [Name] => Bob [Total] => 10 [Wins] => 7 [Losses] => 3 )

Now the question. How do I get the one dimension array $row into the two dimension array $empty_table (replacing the '0' values) , given that $empty_table always has a fixed 5 elements but there can be any number between 0 and 5 '$row's in the while loop depending on what is in the database?

If I try:

$empty_table[] = $row;

or

$empty_table[$row['Name']] = $row;

I only get rows added to the existing array with '0' values, not rows replaced. I do understand that the solution is between the '[]' but I can't figure out put something there that relates to the proper $row[].

2
  • 1
    var_dump($row); and add the result to your question . Commented Jan 12, 2016 at 14:08
  • var_dump($row); gives array(4) { ["Name"]=> string(4) "Bob" ["Total"]=> string(1) "6" ["Wins"]=> string(1) "2" ["Losses"]=> string(1) "4" } Commented Jan 12, 2016 at 16:19

2 Answers 2

2

I think that you can change the array construction to this:

for ($i=0; $i< count($names); $i++) {
     $empty_table[$names[$i]] = array("Name" => $names[$i], "Total" => 0, "Wins" => 0, "Losses" => 0) ;
}

So your array keys will be the name of the person. With this you can identify what row is each moment. The array should be constructed like this:

$empty_table = Array(
  "John"=> Array("name"=>"John", "total"=>10, "wins"=>3, "loses"=>7),
  "Mary"=> Array("name"=>"Mary", "total"=>10, "wins"=>5, "loses"=>5),
);

So you can access to the values like this:

$john_row = $empty_table["John"];
Sign up to request clarification or add additional context in comments.

1 Comment

It might be a bit confusing that $names is both used to make the table (the end result) and to search for something in the database ( WHERE Something = '".$names[$i]."'"; ) These are NOT the same things. in the 'for' loop $names is for the player (Alice, then Bob etc) who plays against x other players (from the same list of names.) In the while part it checks if Alice has played against Alice, if yes, put data in the table, if no, has Alice played against Bob? if yes, put data in table, if no, has Alice played against Charles? etc . then do the same for the 2nd name in the for loop
0

I am just pretending that the $row is from db.

<?php

$names = array('Alice', 'Bob', 'Charles', 'Dick', 'Emy');

for ($i=0; $i< count($names); $i++)
 {
    $empty_table[] = array("Name" => $names[$i], "Total" => 0, "Wins" => 0, "Losses" => 0) ;
 }

$row = array("Name" => 'Alice', "Total" => 5, "Wins" => 6, "Losses" => 4);
$found = false;
foreach($empty_table as $k=>$a) {
    if($a['Name'] == $row['Name']) {
        $found = true;
        break;
    }
}
if($found) {
    echo 'index found'.$k.'\n';
    $empty_table[$k] = array_replace($empty_table[$k], $row);
    var_dump($empty_table);
}

2 Comments

ty, I'll try this when I get home and report later.
Worked like a charm! TY.

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.