Jump to content

How many items can you have in an array?


jamesxg1

Recommended Posts

Hiya!

 

I was wondering how many values are allowed in an array ?

 

This is my code.

 

<?php

class template {

private $parts = array();

function load($parts) {

$sets = array('MAINTITLE', 'MAINCONTENT', 'TITLE', 'CONTAINERTWOTITLE', 'CONTAINERONETITLE', 'CONTAINERONECONTENT', 'CONTAINERTWOCONTENT', 'CONTAINERTHREETITLE', 'CONTAINERTHREECONTENT', 'JQUERY', 'JSSCRIPT');

if(isset($_SESSION['id']) && isset($_SESSION['emailaddress']) && !empty($_SESSION['id']) && !empty($_SESSION['emailaddress']) && strlen($_SESSION['id']) <= 11 && strlen($_SESSION['emailaddress']) <= 150):
$catchname = "SELECT `firstname`, `lastname` FROM `customers` WHERE id = '$sessionid' AND emailaddress = '$sessionemail' LIMIT 1";
$namefetch = mysql_query($catchname);
if($namefetch):
if(mysql_num_rows($namefetch) == 1):
while($name = mysql_fetch_assoc($namefetch)):
$parts['FIRSTNAME'] = $name['firstname'];
$parts['LASTNAME'] = $name['lastname'];
$parts['MENU'] = '<li class="active"><a href="index.php"><b>Home</b></a></li>
		 <li><a href="#"><b>My Account</b></a></li>
		 <li><a href="#"><b>Policys</b></a></li>
		 <li><a href="#"><b>Help</b></a></li>
		 <li><a href="#"><b>Contact Us</b></a></li>';
endwhile;
else:
session_unset();
session_destroy();
$parts['FIRSTNAME'] = 'Guest';
$parts['LASTNAME'] = '';
$parts['MENU'] = '<li class="active"><a href="index.php"><b>Home</b></a></li>
		 <li><a href="#"><b>Login</b></a></li>
		 <li><a href="#"><b>Register</b></a></li>
		 <li><a href="#"><b>Policys</b></a></li>
		 <li><a href="#"><b>About Us</b></a></li>
		 <li><a href="#"><b>Contact Us</b></a></li>
		 <li><a href="#"><b>iDeaz</b></a></li>
		 <li><a href="#"><b>Help</b></a></li>';
endif;
else:
session_unset();
session_destroy();
$parts['FIRSTNAME'] = 'Guest';
$parts['LASTNAME'] = '';
$parts['MENU'] = '<li class="active"><a href="index.php"><b>Home</b></a></li>
		 <li><a href="#"><b>Login</b></a></li>
		 <li><a href="#"><b>Register</b></a></li>
		 <li><a href="#"><b>Policys</b></a></li>
		 <li><a href="#"><b>About Us</b></a></li>
		 <li><a href="#"><b>Contact Us</b></a></li>
		 <li><a href="#"><b>iDeaz</b></a></li>
		 <li><a href="#"><b>Help</b></a></li>';
endif;
else:

$parts['FIRSTNAME'] = 'Guest';
$parts['LASTNAME'] = '';
$parts['MENU'] = '<li class="active"><a href="index.php"><b>Home</b></a></li>
		 <li><a href="#"><b>Login</b></a></li>
		 <li><a href="#"><b>Register</b></a></li>
		 <li><a href="#"><b>Policys</b></a></li>
		 <li><a href="#"><b>About Us</b></a></li>
		 <li><a href="#"><b>Contact Us</b></a></li>
		 <li><a href="#"><b>iDeaz</b></a></li>
		 <li><a href="#"><b>Help</b></a></li>';

if(!file_exists('./assets/template/error.html')):
// update set site shutdown!
else:

if(!is_array($parts)):
	$parts['MAINCONTENT'] = 'We are experiencing difficulty at the present time, please refresh and try again.';
	$parts['MAINTITLE'] = 'Site error';
	$file = file_get_contents('./assets/template/error.html');

	$search = array_keys($parts);
	foreach($search as $key => $value):
		$search[$key] = '{' . $value . '}';
	endforeach;

echo str_replace($search, $parts, $file) . '<br /><br />';
	exit;
else:

if(!file_exists('./assets/template/global.html')):
	$parts['MAINCONTENT'] = 'We are experiencing difficulty at the present time, please refresh and try again.';
	$parts['MAINTITLE'] = 'Site error';
	$file = file_get_contents('./assets/template/error.html');

	$search = array_keys($parts);
	foreach($search as $key => $value):
		$search[$key] = '{' . $value . '}';
	endforeach;

echo str_replace($search, $parts, $file);
	exit;
else:

	$file = file_get_contents('./assets/template/global.html');

$search = array_keys($parts);
	foreach($sets as $item):
		if(!in_array($item, $search)):
		$parts[$item] = '';
		endif;
	endforeach;

	foreach($search as $key => $value):
		$search[$key] = '{' . $value . '}';
	endforeach;

echo str_replace($search, $parts, $file);
$parts = array();
exit;

endif;
endif;
endif;
endif;
}
}
?>

 

The array $sets is not running all of its values through the foreach().

 

Many thanks

 

James.

Link to comment
https://forums.phpfreaks.com/topic/192040-how-many-items-can-you-have-in-an-array/
Share on other sites

Are you sure that the array $sets is not running all of its values through the foreach? You have an if statement inside the loop which might mean nothing gets done for some of the values (specifically, if they're not in the $search array).

Are you sure that the array $sets is not running all of its values through the foreach? You have an if statement inside the loop which might mean nothing gets done for some of the values (specifically, if they're not in the $search array).

 

Im confused! LOL!

The loop is:

 

foreach($sets as $item):
    if(!in_array($item, $search)):
        $parts[$item] = '';
    endif;
endforeach;

 

All of the values in $sets will get looped over, to put it plainly. Unless you were talking about another foreach which does not loop over $sets.

The loop is:

 

foreach($sets as $item):
    if(!in_array($item, $search)):
        $parts[$item] = '';
    endif;
endforeach;

 

All of the values in $sets will get looped over, to put it plainly. Unless you were talking about another foreach which does not loop over $sets.

 

Correct, that is the loop that is not either running all the values in $sets or the array cannot hold more than 10 values.

Looking at your code you are doing alot of repetition, due to lack of logic within your code. I have cleaned up your code and tested it and it doing what it is supposed to be doing.

 

Here is your new code

<?php

class template
{
private $parts = array();

function load($parts)
{
	$record_found = false;
	$sets = array( 'MAINTITLE', 'MAINCONTENT', 'TITLE', 'CONTAINERTWOTITLE', 'CONTAINERONETITLE', 'CONTAINERONECONTENT',
				   'CONTAINERTWOCONTENT', 'CONTAINERTHREETITLE', 'CONTAINERTHREECONTENT', 'JQUERY', 'JSSCRIPT'
				 );

	if( isset($_SESSION['id'], $_SESSION['emailaddress']) &&
		!empty($_SESSION['id']) && !empty($_SESSION['emailaddress']) &&
		strlen($_SESSION['id']) <= 11 && strlen($_SESSION['emailaddress']) <= 150)
	{
		$catchname = "SELECT `firstname`, `lastname` FROM `customers` WHERE id = '$sessionid' AND emailaddress = '$sessionemail' LIMIT 1";
		$namefetch = mysql_query($catchname);

		if($namefetch && mysql_num_rows($namefetch) == 1)
		{
			$record_found = true;
			$name = mysql_fetch_assoc($namefetch);

			$parts['FIRSTNAME'] = $name['firstname'];
			$parts['LASTNAME'] = $name['lastname'];
			$parts['MENU'] = '<li class="active"><a href="index.php"><b>Home</b></a></li>
					 <li><a href="#"><b>My Account</b></a></li>
					 <li><a href="#"><b>Policys</b></a></li>
					 <li><a href="#"><b>Help</b></a></li>
					 <li><a href="#"><b>Contact Us</b></a></li>';
		}
	}

	// we havnt found a matching record, define default data
	if(!$record_found)
	{
		session_unset();
		session_destroy();

		$parts['FIRSTNAME'] = 'Guest';
		$parts['LASTNAME'] = '';
		$parts['MENU'] = '<li class="active"><a href="index.php"><b>Home</b></a></li>
				 <li><a href="#"><b>Login</b></a></li>
				 <li><a href="#"><b>Register</b></a></li>
				 <li><a href="#"><b>Policys</b></a></li>
				 <li><a href="#"><b>About Us</b></a></li>
				 <li><a href="#"><b>Contact Us</b></a></li>
				 <li><a href="#"><b>iDeaz</b></a></li>
				 <li><a href="#"><b>Help</b></a></li>';
	}

	if(!file_exists('./assets/template/error.html'))
	{
		// update set site shutdown!
	}

	if(!is_array($parts)  || !file_exists('./assets/template/global.html'))
	{
		$parts['MAINCONTENT'] = 'We are experiencing difficulty at the present time, please refresh and try again.';
		$parts['MAINTITLE'] = 'Site error';
		$file = file_get_contents('./assets/template/error.html');
	}
	else
		$file = file_get_contents('./assets/template/global.html');

	$search = array_keys($parts);

	foreach($sets as $item)
	{
		if(!in_array($item, $search))
			$parts[$item] = '';
	}

	foreach($search as $key => $value)
	{
		$search[$key] = '{' . $value . '}';
	}

	echo str_replace($search, $parts, $file);
	$parts = array();
}
}

?>

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.