Page 1 of 1
Foreach arrays variable variables
Posted: Sun Aug 27, 2006 12:32 pm
by Dave2000
Code: Select all
$query = "SELECT * FROM login WHERE userid = '$userid'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$ip1 = $row['ip1'];
$ip2 = $row['ip2'];
$ip3 = $row['ip3'];
$time1 = $row['time1'];
$time2 = $row['time2'];
$time3 = $row['time3'];
Echo '<b>Previous Logins:</b>
<table border="1">';
$logins = array('$ip1' => '$time1', '$ip2' => '$time2', '$ip3' => '$time3');
foreach ($logins as $key => $value) { Echo '
<tr><td>'.$key.'</td><td>'.time_since($value,$gmttime).'</td></tr>';
}
Echo '</table></br>';
I am trying to show the IP of, and time since, previous logins in a table. The problem i am having is the first time someone logs in, i dont wish for the second and third row to be shown (as they are empty). I thought the above code might work, but it is not - it's not interpreting the variables (i hope that's the correct word).
First, is it possible to actually do what i am trying by this method - and if so how. If it is not possible could someone suggest an altenative method please.
Thanks
Shears

Posted: Sun Aug 27, 2006 12:40 pm
by blackbeard
Inside the foreach, put in a check for $key. If $key isn't a null value, output the table row.
Posted: Sun Aug 27, 2006 12:42 pm
by volka
Why do you extract the values from an array only to bulid an array from that values?
Variable substitution does not take place in single-quote string literals, see
http://de2.php.net/string
Posted: Sun Aug 27, 2006 1:44 pm
by Dave2000
volka wrote:Why do you extract the values from an array only to bulid an array from that values?
Variable substitution does not take place in single-quote string literals, see
http://de2.php.net/string
All the information is stored within the same row of the table. The new array that is built is a different to the first. I need to build a new key and value - maybe you have a better idea that i could use?
Thank you. Your ideas have improved what i have it - the third key/element (ie. $ip3 and $time3) displays correctly, but not the other two. Any ideas?
Code: Select all
<table border="1">';
$logins = array("$ip1" => "$time1", "$ip2" => "$time2", "$ip3" => "$time3");
foreach ($logins as $key => $value) { Echo '
<tr><td>'.$key.'</td><td>'.time_since($value, $gmttime).'</td></tr>';
}
Echo '</table>';
Thank you,
Shears

Posted: Sun Aug 27, 2006 1:49 pm
by volka
Maybe I wouldn't store the three ip/time pairs in one row.
Anyway
Code: Select all
$query = "SELECT * FROM login WHERE userid = '$userid'";
for($i=1; $i<4; $i++) {
if( $row['ip'.$i] ) {
echo '<tr><td>', $row['ip'.$i], '</td><td>', time_since($row['time'.$i], $gmttime), '</td></tr>';
}
}
Posted: Sun Aug 27, 2006 3:34 pm
by Dave2000
Thank you. That worked - though you forgot...
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
Anyways. I have a question. Why did you initialise $i = 1 and not $i = 0? It works, so I'm not saying what you did is wrong, i just thought that '0' is always the first element in an array.
Thank you again
Shears

Posted: Sun Aug 27, 2006 3:59 pm
by volka
ip1,ip2... not ip0, ip1
Posted: Sun Aug 27, 2006 4:54 pm
by Dave2000
oh yeah

Thank you for all your help
