Jump to content

[SOLVED] how to check if empty array?


ali_2kool2002

Recommended Posts

 

Hi can anyone tel me why the code below wont check if the array is empty im using the if statement  as below in the main code

 

else if( empty ( $_POST['txtbox'] ) )	{
echo "nothings entered into any input text box";
}

 

if i take the if statement it works but i need to somehow say if someone click submit button and non of the input boxes have any data then should say "nothins chosen" ...ne help guys ?? (another thing can some1 tel me how to check the current size of the array ?)

 

 


<?php
session_start();
$PHPSESSID = session_id(); 

require_once('mysql_connect.php');

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

//if ( ! empty ( $_POST['max']) )
//{
//echo "$_POST['max']";
//}

if ( ! empty ( $_POST['txtbox'] ) )
{
foreach ( $_POST['txtbox'] AS $key => $value )
{
	//echo $key . ' = ' . $value . '<br />';
	if($value == ""){
	//echo "non chosen <br />";
	}
	else{
	echo $key . ' = ' . $value . '<br /> added to cart';

	}

	} 

}	else if( empty ( $_POST['txtbox'] ) )	{
echo "nothings chosen";
}


}
?>

Link to comment
https://forums.phpfreaks.com/topic/39182-solved-how-to-check-if-empty-array/
Share on other sites

you have it following a foreach(), so you can't use elseif / else if on a foreach()

 

 

 

<?php
session_start();
$PHPSESSID = session_id(); 

require_once('mysql_connect.php');

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

//if ( ! empty ( $_POST['max']) )
//{
//echo "$_POST['max']";
//}

if ( ! empty ( $_POST['txtbox'] ) )
{
foreach ( $_POST['txtbox'] AS $key => $value )
{
	//echo $key . ' = ' . $value . '<br />';
	if($value == ""){
	//echo "non chosen <br />";
	}
	else{
	echo $key . ' = ' . $value . '<br /> added to cart';

	}
}
}
else if( empty ( $_POST['txtbox'] ) )
{
echo "nothings chosen";
}


}
?>

Just to add..

 

But you really don't need the else if(?), because else would be good enough, because you already tested ! empty(), so if that doesn't return true, then it's empty, so no need for else if()! Also if the form is input='text', the array will still be filled, just the element_key will be empty.

 

 

HI Iv tried that but nothings being displayed ...have i entered you if statement in the rite place?

 


<?php
session_start();
$PHPSESSID = session_id(); 

require_once('mysql_connect.php');




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

if ( ! empty ( $_POST['txtbox'] ) )
{
foreach ( $_POST['txtbox'] AS $key => $value )
{

	if($value == ""){

	}
	else{
	echo $key . ' = ' . $value . '<br /> added to cart';

	}

	} 

}	
}
if (count($_POST['txtbox']) == 0) {
echo "Nothing chosen";
}


?>

According to printf

"Also if the form is input='text', the array will still be filled, just the element_key will be empty."

 

my array when the submit button is pressed will always be full of elements which will be empty spaces ,so i think

that if statement checking if the array is == 0 ? wont really work cuz its always going to be filled up even if it has no characters... so ne more suggestions??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.