Page 2 of 2

Posted: Mon Aug 14, 2006 2:53 am
by tristanlee85
Even if I do split it with explode() or addslashes(), how is it going to know what the date is, the zip code is, or the door number is when I won't always enter in a zipcode?

Posted: Mon Aug 14, 2006 3:49 am
by RobertGonzalez
tristanlee85 wrote:Yeh, I've been using that, but I know how to split it when there is a common explode characher like ":", but with a string like this: 115528209812345418419420421422423424425426427428

Red being the date, blue being the zip, and green being the door numbers.
And why exactly do you suspect that the string would be passed that way?

Posted: Mon Aug 14, 2006 9:34 am
by tristanlee85
Because when I

Code: Select all

echo ${$key};
that's what it outputs.

Posted: Mon Aug 14, 2006 9:51 am
by s.dot
tristanlee85 wrote:Yeh, I've been using that, but I know how to split it when there is a common explode characher like ":", but with a string like this: 115528209812345418419420421422423424425426427428

Red being the date, blue being the zip, and green being the door numbers.
I don't know why it would end up like that......

but

Code: Select all

$strlen = strlen($that_string);
$date = substr($that_string,0,10);
$zip = substr($that_string,10,5);
$door = substr($that_string,$strlen-($strlen-15),$strlen);  // i think
Should get the trick done providing the date and zip are indeed valid.

[edit] Nvm, just read the posts above mine. Fast posters. :P
[edit2] Nvm edit 1. I guess I only read the first page. Back to bed I go.

Posted: Mon Aug 14, 2006 12:10 pm
by RobertGonzalez
tristanlee85 wrote:Because when I

Code: Select all

echo ${$key};
that's what it outputs.
You're going about that the wrong way. As you echo, since there is nothing else being echoed, PHP, as it normally does, is echoing out exactly what you tell it to do. But that stage is not for echoing, it is for assigning. If you want to see exactly what is being output to each var, do this...

Code: Select all

<?php
foreach ($_POST as $key => $value)
{
    // this tells you what is getting assigning to what var
    echo '<p>The var$' . $key . ' will have a value of ' . $value . '...</p>';

    // This is actually handling assignments
    ${$key} = $value;
}
?>

Posted: Thu Aug 17, 2006 9:56 pm
by tristanlee85
I did the "echo" part to see what my results were. I got this:

Code: Select all

The var $date will have a value of 1155610344...

The var $zips418 will have a value of 12345...

The var $door_number418 will have a value of 418...

The var $zips419 will have a value of 12345...

The var $door_number419 will have a value of 419...

The var $zips420 will have a value of 12345...

The var $door_number420 will have a value of 420...

The var $zips421 will have a value of 12345...

The var $door_number421 will have a value of 421...

The var $zips422 will have a value of 12345...

The var $door_number422 will have a value of 422...

The var $zips423 will have a value of 12345...

The var $door_number423 will have a value of 423...

The var $zips424 will have a value of 12345...

The var $door_number424 will have a value of 424...

The var $zips425 will have a value of 12345...

The var $door_number425 will have a value of 425...

The var $zips426 will have a value of 12345...

The var $door_number426 will have a value of 426...

The var $zips427 will have a value of 12345...

The var $door_number427 will have a value of 427...

The var $zips428 will have a value of 12345...

The var $door_number428 will have a value of 428...

The var $post will have a value of ...
That's all good and pretty much what I want. So, since I'm using this loop to post values into a databse, how would I do something like this:

Code: Select all

$query1 = "INSERT INTO misloads VALUES ('','[door_number]','[misload_string]','[unix_date]')";
[door_number] = the value of $door_number***
[misload_string] = the value of $zips***
[unix_date] = the value of $date

Posted: Thu Aug 17, 2006 11:33 pm
by RobertGonzalez
Why not do it as part of the loop? Santize and validate of course, but looping can be used to do what you want.

Posted: Fri Aug 18, 2006 6:08 am
by tristanlee85
That's what I was meaning. Sorry, I had posted the loop code, but edited my post and accidentally removed it. I was meaning something like this:

Code: Select all

foreach ($_POST as $key => $value)
{
    // This is actually handling assignments
    ${$key} = $value;

    $query = "INSERT INTO misloads VALUES ('','[door_number]','[misload_string]','[unix_date]')";
    mysql_query($query);
}
But then of course I'd have to make sure to store the $_POST key $date ouside the loop since it won't be changing sequence if more or less doors are being posted so I can use the variable for the query through the loop. I'd also need to validate the loop so that the key $post is ignored when getting the $_POST values.