0

Suppose I have the following MySQL table result:

ID    price    
-------------
 1     10      
 2     20      
 3     30      


Basically what I want to accomplish is to store these values to a PHP array, and have these values displayed/echoed as a HTML table on a per row basis.

I could do something like:

if($result) {

    $i = 0;

    while ($row = mysql_fetch_array($result)) {
        $id[$i] = $row['id'];
        $price[$i] = $row['price'];      
    }
}

And just have those elements echo together with the HTML table.

However, I also need to have a function that allows the user to delete a row. With that mind I believe I need to have some sort of a 'key' for every row as the identifier for deletion -- a feature which multidimensional array supports.

3
  • What are you asking? Do you want to know how to use a multidimensional array? Commented Oct 19, 2011 at 6:28
  • @skyuzo Quite honesly I'm not sure which 'type' of array should be used. All I know is i need to organize my results (the simple example above should do) using an array in such a way that I could delete a 'row' from that array. Commented Oct 19, 2011 at 6:38
  • From 'that array' or from the database? what's the point in deleting a row from array? To let you know, this array already doesn't exist by the time user sees his table. Commented Oct 19, 2011 at 6:49

2 Answers 2

1

There's nothing preventing you from using a multi dimensional array and using one of the unique values as an index:

// Store result
$data = array();
if($result) {
    while ($row = mysql_fetch_array($result)) {
        $data[$row['raiser_id']] = $row;
    }
}


// Building table
echo "<table>";
foreach ($data as $row)
{
    echo "<tr>";
    echo "<td>" . $row['raiser_id'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['fcr'] . "</td>";
    echo "<td>" . $row['date_of_application'] . "</td>";
    echo "<td>" . $row['no_of_heads'] . "</td>";
    echo "<td>" . $row['place_of_farm'] . "</td>";
    echo "</tr>";
}
echo "</table>";


// Removing an entry by raiser_id
$raiser_id = 10;
if (!empty($data[$raiser_id]))
{
    unset($data[$raiser_id]);
    echo "Removed entry";
}
else
{
    echo "No entry to remove";
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was aware that arrays can take various values as indexes, but your suggest to use $row['id'] as the index was a nice touch. My code is working now thanks!
1

To delete a row from the database, you have to have another PHP script and have to call it using POST method and run an SQL query in it.

If you are talking of just displaying this table, PHP has nothing to do here then - go for JavaScript.

By the time a user sees his table, there is no mysql result, no PHP array and no whole PHP running either. It's all dead long time ago. Keep that in mind.

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.