Php associative array

Information about lab:

  1. Create a two dimensional array (the second dimension should be associative, with keys Name, Karma, and LastLogin, the first can be indexed (1-4). Or you can have both dimensions be associative) that contains the following information about users of your website:
  2. Print out the array sorted by user-id
  3. Print out the array sorted by Login (use the strtotime() function)
  4. Print out only users who have Karma score > 10, sorted by Karma score in descending order.
  5. Add a form to your php file that has a user put in a name and id, with the method post.
  6. If the name exists, add 5 to the karma score. If the name does not, add an element to the array with karma score of 1, and today’s date (use the date function). Print out the new array.

I have done most of the lab i just didnt do the part where im supposed to print out the sorted data.

Code:

$User_name = $_POST["Name"];

$ID = $_POST["UserID"];

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

for($i = 0; $i < 4; $i++)
{	
	if ($loginTable['Name'][i] === $User_name){
		$loginTable['Karma Score'][i] += 5;		
			
	}
	else
		array_push($loginTable["UserID"],$ID);
        array_push($loginTable["Name"],$User_name);
        array_push($loginTable["KarmaScore"],1);
        array_push($loginTable["LastLogin"],"today");
}
/*
function timeNow($time)
{
	$time = time();
	echo (date("d-m-Y", $time));
	
	
}
*/
echo "<table border ='2px black solid'>";
for ($x = 0; $x < 4; $x++){
echo "<tr>";
echo "<td>".$loginTable["UserID"][$x]."</td>";
echo "<td>".$loginTable["LastLogin"][$x]."</td>";
echo "<td>".$loginTable["Name"][$x]."</td>";
echo "<td>".$loginTable["KarmaScore"][$x]."</td>";
echo "</tr>";
}
echo"</table>";

Right now i was just trying to see if what i have so far is working properly and when i ran the program i entered a name that is not part of the array and the ID number and noticed the karma score did not change and also for the time i just placed “today” just as a place holder because i am also having problems with the time function i created to record the time as well and that was not replaced as well with the use of the array_push…

Have a look in the code for that section, you’ll see a typo that would stop it working as you intended.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.