添加到多维数组中的值

时间:2015-03-01 06:38:57

标签: php arrays multidimensional-array

我的脚本有点麻烦。我做了一个简单的表单,我想要它做的是,当用户输入他的名字,并且名字已经在数组中......他的业力得分将上升5但是我正在努力添加部分,我无法让它工作。

非常感谢任何帮助或建议。提前谢谢!

<html>

<head></head>

<body>

<?php

$karma_score = array(   array("Userid" => 1, "NameID" => 'Doe', "Karma" => 45, "LastLogin" => "2012-08-30"),
                        array("Userid" => 2, "NameID" => 'Smith', "Karma" => 123, "LastLogin" => "2012-09-02"),
                        array("Userid" => 3, "NameID" => 'Chan', "Karma" => 1, "LastLogin" => "2011-12-23"),
                        array("Userid" => 4, "NameID" => 'Zee', "Karma" => 15, "LastLogin" => "2012-07-01"));

function printArray($a){
    echo '<table border="1px">';
    echo '<tr>';
        foreach(array_keys($a[0]) as $head){
            echo '<th>'.$head.'</th>';
        }
    echo '</tr>';

    foreach($a as $b) {
    echo '<tr>';
        echo '<td>'.$b['Userid'].'</td>';
        echo '<td>'.$b['NameID'].'</td>';
        echo '<td>'.$b['Karma'].'</td>';
        echo '<td>'.$b['LastLogin'].'</td>';
    echo '</tr>';
    }
    echo '</table>';
}

function findInfo($a){
    $InputName = $_POST['name'];

    for($counter = 0; $counter < 4; $counter++){
        foreach($a as $b) {
            if ($b['NameID'] === $InputName){
                // I would assume here is where the calculation would be done...
            }
        }
    }
printArray($karma_score);
}

while(isset($_POST['submit'])){
    findInfo($karma_score);
}

?>

<br>
<form action="" method="POST">
Name: <input type="text" name="name" placeholder="Enter Name Here">
ID: <input type="text" name="id" placeholder="Enter ID #">
<input type="submit" name="submit">

</body>

</html>

1 个答案:

答案 0 :(得分:1)

您的代码以及我的意见和建议:

<html>

<head></head>

<body>

<?php

// I am not sure why you have this variable here, But
// This variable will be initialized every time this page is loaded
// What it means is that this data won't be saved/updated for different users
// If you want data to keep updating across the web, you need to use a database
$karma_score = array(array("Userid" => 1, "NameID" => 'Doe', "Karma" => 45, "LastLogin" => "2012-08-30"),
    array("Userid" => 2, "NameID" => 'Smith', "Karma" => 123, "LastLogin" => "2012-09-02"),
    array("Userid" => 3, "NameID" => 'Chan', "Karma" => 1, "LastLogin" => "2011-12-23"),
    array("Userid" => 4, "NameID" => 'Zee', "Karma" => 15, "LastLogin" => "2012-07-01"));

function printArray($a)
{
    echo '<table border="1px">';
    echo '<tr>';
    foreach (array_keys($a[0]) as $head) {
        echo '<th>' . $head . '</th>';
    }
    echo '</tr>';

    foreach ($a as $b) {
        echo '<tr>';
        echo '<td>' . $b['Userid'] . '</td>';
        echo '<td>' . $b['NameID'] . '</td>';
        echo '<td>' . $b['Karma'] . '</td>';
        echo '<td>' . $b['LastLogin'] . '</td>';
        echo '</tr>';
    }
    echo '</table>';
}

// It seems like you are always passing
// $karma_score in here. If you want the original
// variable to always be updated, you need to pass
// it by reference like so:
// function findInfo(&$a)
function findInfo($a)
{
    // In the last line of the function
    // you are printing the variable $karma_score
    // Since, it's the global variable, you cannot directly use
    // it in here. You need to pull it in the function like so:
    // global $karma_score;

    $InputName = $_POST['name'];

    for ($counter = 0; $counter < 4; $counter++) {
        foreach ($a as $b) {
            if ($b['NameID'] === $InputName) {
                // I would assume here is where the calculation would be done...
            }
        }
    }
    printArray($karma_score);
}
// This is an infinite loop
// It should be if instead of a while
while (isset($_POST['submit'])) {
    // Since you already have $karma_score as a global variable
    // You don't need to pass it here
    findInfo($karma_score);
}

?>

<br>

<form action="" method="POST">
    Name: <input type="text" name="name" placeholder="Enter Name Here">
    ID: <input type="text" name="id" placeholder="Enter ID #">
    <input type="submit" name="submit">

</body>

</html>

这可能就是你要找的东西:

<html>

<head></head>

<body>

<?php

$karma_score = array(array("Userid" => 1, "NameID" => 'Doe', "Karma" => 45, "LastLogin" => "2012-08-30"),
    array("Userid" => 2, "NameID" => 'Smith', "Karma" => 123, "LastLogin" => "2012-09-02"),
    array("Userid" => 3, "NameID" => 'Chan', "Karma" => 1, "LastLogin" => "2011-12-23"),
    array("Userid" => 4, "NameID" => 'Zee', "Karma" => 15, "LastLogin" => "2012-07-01"));

function printArray($a)
{
    echo '<table border="1px">';
    echo '<tr>';
    foreach (array_keys($a[0]) as $head) {
        echo '<th>' . $head . '</th>';
    }
    echo '</tr>';

    foreach ($a as $b) {
        echo '<tr>';
        echo '<td>' . $b['Userid'] . '</td>';
        echo '<td>' . $b['NameID'] . '</td>';
        echo '<td>' . $b['Karma'] . '</td>';
        echo '<td>' . $b['LastLogin'] . '</td>';
        echo '</tr>';
    }
    echo '</table>';
}

function findInfo()
{
    global $karma_score;

    $InputName = $_POST['name'];

        foreach ($karma_score as $key => $b) {
            if ($b['NameID'] === $InputName) {
                $karma_score[$key]["Karma"] += 5;
            }
        }

    printArray($karma_score);
}

if (isset($_POST['submit'])) {
    findInfo();
}

?>

<br>

<form action="" method="POST">
    Name: <input type="text" name="name" placeholder="Enter Name Here">
    ID: <input type="text" name="id" placeholder="Enter ID #">
    <input type="submit" name="submit">

</body>

</html>