Getting loop-generated values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Because when I

Code: Select all

echo ${$key};
that's what it outputs.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
}
?>
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post 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.
Post Reply