Jump to content

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


ScottRiley

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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.