Jump to content

Recommended Posts

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 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.
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]
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
try[code]
<?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>";
}
?>
[/code]
Without the array

[code]<?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/>";
}

?>  [/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.