Jump to content


Photo

Array Upper Bounds (and a small bit of code validation)


  • Please log in to reply
7 replies to this topic

#1 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 10:39 AM

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.

<?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'];
		}
	}
?>
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 it
3. If this doesn't, don't echo it
4. Move to next $d
5. Repeat steps 1-4 until $d=$rowsaffected (or the array upper bound)
6. $i = $i+1
7. 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.

#2 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 10:56 AM

I also came up with this code, does this seem right?:

<?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]);
				}
			}
		}
			
	}
?>


#3 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 11 August 2006 - 01:08 PM

can yuo post structure of table 'southport_categories'

#4 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 11 August 2006 - 01:23 PM

id----Category-----subcat
1----Sport---------football
2----Food---------ice cream
3----Sport---------Hockey
4----Clothes-------Dress
5----Food----------Biscuits

that's basicall the structure, although with different category names that I can't be bothered finding :P

#5 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 11 August 2006 - 02:04 PM

try
<?php
include"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>";
}
?>


#6 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 13 August 2006 - 05:40 PM

Thanks a lot, that's a lot easier to comprehend than my crap, plus...it'll actually work  :D

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 August 2006 - 06:34 PM

Without the array

<?php

include"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/>";
}

?> 

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 14 August 2006 - 10:39 AM

Thanks a lot guys




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users