ScottRiley Posted August 11, 2006 Share Posted August 11, 2006 Is there a way to determine the upper bound of an array in PHP. I want to do a for statement ($i=0; $i < (an arrays upper bound); $i++). I basically want to store the data of an sql table in arrays. So, what I think I could do is.[code]<?php $sql="SELECT * FROM southport_categories" $query=mysql_query($sql); $rows=mysql_num_rows($sql); while($con=mysql_fetch_array{$query)) { for($1=0; $i<$rows; $i++) { $scat[i]=$con['subcategory']; $cat[i]=$con['category']; } }?>[/code]Would that work? Instead of using the upper bound, use the number of rows affected by the query (which, if it is selecting everything, will be the abount of data in the table, i.e. the upper bound of the array). I will then create 2 arrays and store the category and subgategory.My table contains categories and subcategories. All subcategories are unique, but can fall under the same category. What I'm hoping to do is to run a few 'for' statements, where I will echo the main category, and, if the category corresponding to a certain subcategory is identical to that of the category it is being checked against, I want to output the subcategory below the main one.Essentially I want to:For each index in the category array, check if the 'subcategory' in the database, relating to the current index of the subcat array, check if they are the same, if not, move on to the next subcategory. An algorithm would look something like:1. Query the database, selecting the 'category' where subcat='subcat[d]'2. If this matches, the category[i], echo it3. If this doesn't, don't echo it4. Move to next $d5. Repeat steps 1-4 until $d=$rowsaffected (or the array upper bound)6. $i = $i+17. Repeat steps 1-5 until $i=$rowsaffected (or the array upper bound)Am I going about this the right way, I hate using while statements, especially with the SQL I'll be using, I find it hard to keep track of, I mich prefer for statements, thats why I want to put them all into the $cat and $scat arrays and do for loops on the indicies of them. Quote Link to comment https://forums.phpfreaks.com/topic/17225-array-upper-bounds-and-a-small-bit-of-code-validation/ Share on other sites More sharing options...
ScottRiley Posted August 11, 2006 Author Share Posted August 11, 2006 I also came up with this code, does this seem right?:[code]<?php include"connect.php" $sql="SELECT * FROM southport_categories" $query=mysql_query($sql); $rows=mysql_num_rows($sql); while($con=mysql_fetch_array{$query)) { for($1=0; $i<$rows; $i++) { $scat[i]=$con['subcategory']; $cat[i]=$con['category']; } } for($i=0; $i<$rows; $i++) //NESTED ITERATION STATEMENTS GALORE!!! { echo($cat[i]); for($d=0; $d<$rows; $d++) { $subcat = $scat[d]; $catfromsubcat="SELECT category FROM southport_categories WHERE subcat='$subcat'"; $cfsquery=mysql_query($catfromsubcat); $cfsrows=mysql_num_rows($cfsquery); while($cfsarray=mysql_fetch_array($cfsquery)) { if($cfsarray['category']=category[i]) { echo($category[i]); } } } }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17225-array-upper-bounds-and-a-small-bit-of-code-validation/#findComment-73005 Share on other sites More sharing options...
sasa Posted August 11, 2006 Share Posted August 11, 2006 can yuo post structure of table 'southport_categories' Quote Link to comment https://forums.phpfreaks.com/topic/17225-array-upper-bounds-and-a-small-bit-of-code-validation/#findComment-73060 Share on other sites More sharing options...
ScottRiley Posted August 11, 2006 Author Share Posted August 11, 2006 id----Category-----subcat1----Sport---------football2----Food---------ice cream3----Sport---------Hockey4----Clothes-------Dress5----Food----------Biscuitsthat's basicall the structure, although with different category names that I can't be bothered finding :p Quote Link to comment https://forums.phpfreaks.com/topic/17225-array-upper-bounds-and-a-small-bit-of-code-validation/#findComment-73065 Share on other sites More sharing options...
sasa Posted August 11, 2006 Share Posted August 11, 2006 try[code]<?phpinclude"connect.php";$sql="SELECT * FROM southport_categories";$query=mysql_query($sql);while ($row=mysql_fetch_array($query)) $output[$row['Category']][]=$row['subcat'];foreach ($output as $cat => $subcats){ echo "<p>$cat"; foreach ($subcats as $subcat) echo " -> $subcat"; echo "</p>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17225-array-upper-bounds-and-a-small-bit-of-code-validation/#findComment-73089 Share on other sites More sharing options...
ScottRiley Posted August 13, 2006 Author Share Posted August 13, 2006 Thanks a lot, that's a lot easier to comprehend than my crap, plus...it'll actually work :D Quote Link to comment https://forums.phpfreaks.com/topic/17225-array-upper-bounds-and-a-small-bit-of-code-validation/#findComment-74129 Share on other sites More sharing options...
Barand Posted August 13, 2006 Share Posted August 13, 2006 Without the array[code]<?phpinclude"connect.php";$sql = "SELECT category, subcat FROM southport_categories ORDER BY category, subcat";$query=mysql_query($sql);$prevCat = '';while (list($cat, $scat) = mysql_fetch_row($query)) { if ($cat != $prevcat) { echo "<p>$cat</p>"; $prevCat = $cat; } echo "-> $scat<br/>";}?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/17225-array-upper-bounds-and-a-small-bit-of-code-validation/#findComment-74150 Share on other sites More sharing options...
ScottRiley Posted August 14, 2006 Author Share Posted August 14, 2006 Thanks a lot guys Quote Link to comment https://forums.phpfreaks.com/topic/17225-array-upper-bounds-and-a-small-bit-of-code-validation/#findComment-74401 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.