jamesxg1 Posted February 14, 2010 Share Posted February 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192040-how-many-items-can-you-have-in-an-array/ Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/192040-how-many-items-can-you-have-in-an-array/#findComment-1012132 Share on other sites More sharing options...
jamesxg1 Posted February 14, 2010 Author Share Posted February 14, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/192040-how-many-items-can-you-have-in-an-array/#findComment-1012133 Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192040-how-many-items-can-you-have-in-an-array/#findComment-1012135 Share on other sites More sharing options...
jamesxg1 Posted February 14, 2010 Author Share Posted February 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192040-how-many-items-can-you-have-in-an-array/#findComment-1012137 Share on other sites More sharing options...
jamesxg1 Posted February 14, 2010 Author Share Posted February 14, 2010 B to the U to the M to the P. LOL! Quote Link to comment https://forums.phpfreaks.com/topic/192040-how-many-items-can-you-have-in-an-array/#findComment-1012157 Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/192040-how-many-items-can-you-have-in-an-array/#findComment-1012162 Share on other sites More sharing options...
wildteen88 Posted February 14, 2010 Share Posted February 14, 2010 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(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/192040-how-many-items-can-you-have-in-an-array/#findComment-1012178 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.