View Full Version : Need help with simple array loop
Glass Casket
07-06-2006, 03:21 PM
So I have the following code:
function check_blank() {
$options = array($order_number);
for($i = 0; $i < count($options); $i++) {
if(strlen($options[$i]) == 0) {
$options[$i] = $na;
}
}
}
The loop only has one element right now, but usualy has over 20. It's simply suppose to go though each array element(which are form fields), and check if they're empty or not. If they're empty, I assign $na ("N/A") to the value. It's not it's not blank, leave the variable as is.
I've been at this for a while, trying a lot of different things.
Thanks!
Brandoe85
07-06-2006, 03:29 PM
Where are you assigning $na?
Glass Casket
07-06-2006, 03:30 PM
I'm assigning $na on the top of the page.
Brandoe85
07-06-2006, 03:33 PM
OK, you'll have to use $GLOBALS to access it inside your function, like:
$GLOBALS['na']
Good luck;
Glass Casket
07-06-2006, 03:37 PM
Thanks man! That fixed that problem.
But now it seems that no matter if the field is empty or full, it always thinks strlen is 0. :\
Brandoe85
07-06-2006, 03:43 PM
Looks like you may need to use $GLOBALS with $order_number as well?
But the code doesn't really make sense. You're declaring options as a new array, but you're never assigning anything to the elements before you loop it. Therefore, strlen of nothing is 0.
Glass Casket
07-06-2006, 03:50 PM
You're right, from what I posted above. The code should behave like it is. I changed $order_number to a global variable and all is working now (I was fetching the variable on top the my script). Is this really the best approach to my problem (I'll have 25 global variables)?
Thank you.
Brandoe85
07-06-2006, 04:04 PM
You're welcome :)
I don't see a need for the $na variable. Unless, of course, you're using it in other places and changing the value when needed or something. Where are you filling your array of form fields, is it all the form fields your validating? Why would you have 25 globals?
Glass Casket
07-06-2006, 04:10 PM
Yes, they're all form fields. And I would need 25 global variables because the form has 25 fields. :)
But I just found thaty I can use 'global $1, $2...'
Brandoe85
07-06-2006, 04:15 PM
You don't need 25 globals, you can loop through $_POST, as it's an array already, take a look at this thread:
http://www.codingforums.com/showthread.php?t=79173
Let us know how it goes.
Good luck;
Glass Casket
07-06-2006, 06:40 PM
That works now where blank fields are made "N/A". But when I try to insert $order_number into the database, it simply enters nothing.
global $order_number;
$options = array($order_number);
for($i = 0; $i < count($options); $i++) {
if(strlen($options[$i]) == 0) {
$options[$i] = "N/A";
}
}
Thank you
Brandoe85
07-06-2006, 06:49 PM
What is $order_number?
kehers
07-06-2006, 06:51 PM
Can u post the code for the insertion?
Glass Casket
07-06-2006, 06:54 PM
$order_number is a form field.
$sql_query = mysql_query("INSERT INTO tablename (orderNo) VALUES ('$order_number')", $dbConnect) or die(mysql_error());
kehers
07-06-2006, 07:20 PM
Sorry, but u may av to post more of the code to really get it, especially the whole query area and probably part of the order_number value in the form.
On the other hand, u can do dis simple check to see if there is eally a value in the variable $order_number
echo "order number is $order_number";
if something shows up, then we know the problem is with the sql insertion area and not that $order_number doesnt contain something
Glass Casket
07-06-2006, 07:49 PM
Nothing shows up when I do that. :(
But when I enter a value in that field, it goes in the database fine. It's only when the field is left blank that it goes into the database as empty.
$order_number = $_GET["order_number"];
function check_blank() {
global $order_number;
$options = array($order_number);
for($i = 0; $i < count($options); $i++) {
if(strlen($options[$i]) == 0) {
$options[$i] = "N/A";
} else {
echo "+ ";
}
}
}
$sql_query = mysql_query("INSERT INTO tablename (orderNo) VALUES ('$order_number')", $dbConnect) or die(mysql_error());
kehers
07-06-2006, 09:22 PM
ok then the problem is that the variable isnt getting anything from where is coming from. check back ur form again and mack sure all things are well set.
You may also post the form code
thunderhoster
07-06-2006, 11:42 PM
Yes, we need to see more of the code.
Still you can try to check out if you're using get or post method in the form action attribute or if the field's name is really order_number.
Glass Casket
07-07-2006, 02:47 AM
Sure, I'll post the code when I'm on the right computer.
But I'm fairly sure the variables are getting though because when I alert the url I pass with AJAX, the values are all in there. And the values do get entered if I don't leave them blank.
digital-ether
07-07-2006, 04:18 AM
Sure, I'll post the code when I'm on the right computer.
But I'm fairly sure the variables are getting though because when I alert the url I pass with AJAX, the values are all in there. And the values do get entered if I don't leave them blank.
Hi Glass Casket,
I think your understanding of variable scope in PHP may be a bit affected by JavaScript.
JS will check for a variable/property in the scrope of the function is is being called, and if it doesnt exist, go up the prototype chain and thus check if the variable is available in the scope of the parent.
In this way, JS finally reaches the "global scope" and ends there, or returns the first instance of the variable it finds along the prototype chain.
In PHP, this does not happen. PHP only checks for the variables existance in the local scope of a function and ends. Even if the variable exists in the Global scope, the function will not 'see' it. The exception being when accessing the globals variables, like the $GLOBALS array, or using the "global" modifier in your function to make your function check for the varialbe in the global scope.
eg:
function foo() {
global $var; // look for $var in the global scope
$GLOBALS['var']; // $GLOBALS exists in all scope
}
Knowing this, you should always pass the value of your needed variables to php functions. Making variable available to a function from teh global scope should be used less on needed variables, and maybe only on needed configurations, or data needed to run your function seperate from the changing variables you pass it.
In your situation, I would recommend:
function check_blank($options /* use this */) {
//global $order_number; // dont use this
//$options = array($order_number); // this should already be an array
$count = count($options); // now you count() only once
for($i = 0; $i < /* count($options) */ $count; $i++) {
if(strlen($options[$i]) == 0) {
$options[$i] = "N/A";
} else {
echo "+ ";
}
}
}
check_blank($_GET["order_number"]); // call your function
These are only suggestions.
I assume $_GET["order_number"] is any array?
If it isn't then the function is useless, since it is only supplied a single variable. Putting array($order_number) still means your loop will only execute once if $order_number is not an array (or object).
If you're wondering how to construct a url where order_number would be sent as an array to the server, theres the syntax.
http://example.com/script.php?order_number[]=value1&order_number[]=value2
In an html form this would mean you name your form elements with order_number[]
Hope that helps... :)
Glass Casket
07-07-2006, 12:34 PM
Wow, thanks a lot for that in depth reply digital-ether. :)
I'll be trying it out once I get on the right computer.
Thank you!
Glass Casket
07-07-2006, 04:53 PM
I can't seem to get it to work properly. It keeps entering 'Array' in the database. :(
HTML form:
<li><label for="order_number">Order #</label><input type="text" maxlength="10" name="order_number" id="order_number[]" /></li>
AJAX:
var order_number = document.getElementById("order_number[]").value;
var url = "/refurb/barriemetals/me_insert.php?order_number[]=" + order_number;
PHP script
$order_number = $_GET["order_number"];
function check_blank($options) {
$count = count($options);
for($i = 0; $i < $count; $i++) {
if(strlen($options[$i]) == 0) {
$options[$i] = "N/A";
} else {
echo "+ ";
}
}
}
check_blank($order_number);
$sql_query = mysql_query("INSERT INTO barriemetals (orderNo) VALUES ('$order_number')", $dbConnect) or die(mysql_error());
The URL gets the information from the form field because when I do alert(url), I can see the value I entered.
Thanks.
Glass Casket
07-07-2006, 07:12 PM
I've been playing with it some more, and still can't seem to figure it out.
Any ideas?
thunderhoster
07-07-2006, 07:37 PM
try
$order_number = $_GET["order_number"];
function check_blank($options) {
$count = count($options);
for($i = 0; $i < $count; $i++) {
if(strlen($options[$i]) == 0) {
$options[$i] = "N/A";
} else {
echo "+ ";
}
}
return $options;
}
$order_number = check_blank($order_number);
$sql_query = mysql_query("INSERT INTO barriemetals (orderNo) VALUES ('$order_number')", $dbConnect) or die(mysql_error());
Glass Casket
07-07-2006, 07:48 PM
Nice man, that did the trick. Except I had to put 'return $options [$i];' in the for loop. :)
Thanks!
Edit: Instead of doing '$variable_name = check_blank($variable_name);' for every variable. Would there be a faster way? Liek perhaps looping through all the $_GET variables?
Glass Casket
07-07-2006, 08:20 PM
Also, as soon as I start using that function. It seems that only 1 character of every fields actually gets entered into the database. :\
Edit: N/A goes into the database fine. It's only when I type in a form field value that only one characters enters the database.
Brandoe85
07-07-2006, 08:48 PM
I still think there's a better way.
I originally directed you to a thread about looping through $_POST. The cencept should be the same.
Good luck;
thunderhoster
07-07-2006, 08:48 PM
If you put the return in the for, it will only loop once, it has to be outside the for loop.
Right now I have no time to figure that out, but if you want, add me to your msn and tomorrow I can see a better way of doing that.
Sorry.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.