Author Topic: How many items can you have in an array?  (Read 552 times)

0 Members and 1 Guest are viewing this topic.

Offline jamesxg1Topic starter

  • Devotee
  • Posts: 1,113
  • Gender: Male
  • Every action has a re-action.
    • View Profile
    • Your Homely
How many items can you have in an array?
« on: February 14, 2010, 09:46:37 AM »
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.
The human race would be nothing without knowledge.

Offline salathe

  • Lazy
  • Administrator
  • Addict
  • *
  • Posts: 1,540
  • Gender: Male
  • Temperament: Snuggly
    • View Profile
    • My Blog
Re: How many items can you have in an array?
« Reply #1 on: February 14, 2010, 09:57:46 AM »
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).
PHP Documentation — Read it, or the badger will come for you.
http://php.net/manual/

Offline jamesxg1Topic starter

  • Devotee
  • Posts: 1,113
  • Gender: Male
  • Every action has a re-action.
    • View Profile
    • Your Homely
Re: How many items can you have in an array?
« Reply #2 on: February 14, 2010, 09:59:42 AM »
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 human race would be nothing without knowledge.

Offline salathe

  • Lazy
  • Administrator
  • Addict
  • *
  • Posts: 1,540
  • Gender: Male
  • Temperament: Snuggly
    • View Profile
    • My Blog
Re: How many items can you have in an array?
« Reply #3 on: February 14, 2010, 10:02:29 AM »
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.
PHP Documentation — Read it, or the badger will come for you.
http://php.net/manual/

Offline jamesxg1Topic starter

  • Devotee
  • Posts: 1,113
  • Gender: Male
  • Every action has a re-action.
    • View Profile
    • Your Homely
Re: How many items can you have in an array?
« Reply #4 on: February 14, 2010, 10:04:14 AM »
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.
The human race would be nothing without knowledge.

Offline jamesxg1Topic starter

  • Devotee
  • Posts: 1,113
  • Gender: Male
  • Every action has a re-action.
    • View Profile
    • Your Homely
Re: How many items can you have in an array?
« Reply #5 on: February 14, 2010, 11:05:42 AM »
B to the U to the M to the P. LOL!
The human race would be nothing without knowledge.

Offline salathe

  • Lazy
  • Administrator
  • Addict
  • *
  • Posts: 1,540
  • Gender: Male
  • Temperament: Snuggly
    • View Profile
    • My Blog
Re: How many items can you have in an array?
« Reply #6 on: February 14, 2010, 11:27:03 AM »
Arrays can hold many, many more than 10 items and foreach loops will loop over all of the values (unless told otherwise). What makes you think that the loop is not looping over all of the array?
PHP Documentation — Read it, or the badger will come for you.
http://php.net/manual/

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: How many items can you have in an array?
« Reply #7 on: February 14, 2010, 11:59:45 AM »
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
Code: [Select]
<?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();
}
}

?>