print error if array is set to [0]

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

Post Reply
Isaiaha Starr
Forum Newbie
Posts: 3
Joined: Wed Feb 11, 2009 1:57 pm

print error if array is set to [0]

Post by Isaiaha Starr »

Hi

I'm very new at php and wondering if someone could point me in the right direction.

Our code retains the value of the array when submitted (if form has to be re-displayed with errors) however I'm having a hard time having it print an error if the field is not selected (in this case... $months still says 'Select Month' or 0)

$months = array ('Select Month','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

$sticky_months='';
if(isset($_POST['Submitted'])) {

$sticky_months = $_POST['month'];

}


echo '<select id="size-1" name="month">';
foreach ($months as $key => $value) {

if($key == $sticky_months) {
echo '<option value="' . $key .'" selected>' . $value . '</option>';
}else{
echo '<option value="' . $key .'">' . $value . '</option>';
}
}
echo '</select>';


//for loop for days - drop down menu

$sticky_day='';
if(isset($_POST['Submitted'])) {

$sticky_day = $_POST['day'];

}

echo '<select id="size-2" name="day">';
for ($day = 1; $day <=31; $day++) {
//echo "<option value=\"$day\">$day</option>";

if($day == $sticky_day) {
echo '<option value="' . $day .'" selected>' . $day . '</option>';
}else{
echo '<option value="' . $day .'">' . $day . '</option>';
}
}

echo '</select>';


if anyone has any ideas, I'd appreciate it

I have tried adding all sorts of isset and
if($months = ['0']) {
$errors[]= '<li class="error-list">Shirt Size</p>';
}
.. that error array works great for all my other fields...

and with the if($_POST['month'] && $months = ['0']... but got all kinds of neat errors...

Thank you in advance

Thank you
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: print error if array is set to [0]

Post by requinix »

"Select Month" is a rather weird name for a month, isn't it?

How about this:
Remove that from the list of months and directly insert it into the HTML. Then use in_array to see if $sticky_months is in the $months array. If so, good. If not, error.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: print error if array is set to [0]

Post by watson516 »

[ code=php]...[ /code]
Isaiaha Starr
Forum Newbie
Posts: 3
Joined: Wed Feb 11, 2009 1:57 pm

Re: print error if array is set to [0]

Post by Isaiaha Starr »

Thank you for your input

I have tried this and part of it works great however the in_array is always set to false... I think it's because the $sticky_months = $_POST['month'] makes everything is that drop down list a part of sticky_months?


Here is the new code

$months = array (1=>'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

if(isset($_POST['Submitted'])) {

$sticky_months = $_POST['month'];

}


echo '<select id="size-1" name="month">';
echo '<option value="" >Select Month</option>';

foreach ($months as $key => $value) {

if($key == $sticky_months) {
echo '<option value="' . $key .'" selected>' . $value . '</option>';
}else{
echo '<option value="' . $key .'">' . $value . '</option>';

}
}

echo '</select>';

****** this works great ****

here is the validation that always says false...

if (in_array("$sticky_months", $months)) {

$sticky_months = $_POST['month'];

}else{

$errors[]='<li class="error-list">Months</p>';

}


Thank you for your previous post... getting closer just a bit over my head
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: print error if array is set to [0]

Post by John Cartwright »

Please use the appropriate tags when posting code blocks in the forums. Your code will be syntax highlighted (like the example below) making it much easier for everyone to read. You will most likely receive more answers too!

Simply place your code between tags, being sure to remove the spaces. You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:
  1. Forum Rules
  2. General Posting Guidelines
  3. Posting Code in the Forums
If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the php manual online. You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member. Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:

Code: Select all

<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: print error if array is set to [0]

Post by requinix »

in_array — Checks if a value exists in an array
Note how it says "value" and not "key".

If you want to do it with keys instead try isset.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: print error if array is set to [0]

Post by John Cartwright »

Isaiaha Starr
Forum Newbie
Posts: 3
Joined: Wed Feb 11, 2009 1:57 pm

Re: print error if array is set to [0]

Post by Isaiaha Starr »

Thank you for your help! I thought I was using values for in_array but obviously I wasn't... so I tried your suggestion and it works wonderfully!

Here is the final code that works (sorry about the tags)

Code: Select all

 
 <?php
 
$months = array (1=>'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
 
if(isset($_POST['Submitted'])) {
    $sticky_months = $_POST['month'];
}
 
echo '<select id="size-1" name="month">';
echo '<option value="sm" >Select Month</option>';
 
foreach ($months as $key => $value) {
    if($key == $sticky_months) {
        echo '<option value="' . $key .'" selected>' . $value . '</option>';
}else{
        echo '<option value="' . $key .'">' . $value . '</option>';
}
}
 
if(isset($_POST['month']) && $_POST['month'] == 'sm') {
                     $errors[]= '<li class="error-list">Month - Date of Birth</p>';
}
?>
 
Blessings for a great day!
Post Reply