Jump to content


Photo

Foreach Explode array


  • Please log in to reply
2 replies to this topic

#1 dharm

dharm
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 11 August 2006 - 10:55 AM

Hi, im a noob to this and would like some help with sorting categories. I have a table with 2 columns CATS and SUBCATS.

--------------------------------------
| CATS  | SUBCATS                      |
--------------------------------------
| c1      | sub1.1, sub1.2, sub1.3    |
------------------------------------ |
| c2      | sub2.1, sub2.2, sub2.3,    |
|          | sub2.4                          |
-------------------------------------                               
| c3      | sub3.1, sub3.2 ....
-------------------------------------

A user selects a main category from a previous page(see below for code) then this data is passed to here where I want to use the categories row number and I want to explode a selected row from SUBCATS and divide all the content where “,” is and sort them within radiobuttons. Here is the code I have:

echo "<strong>$cat</strong><br><p>";
  
$query1 = "Select * from ".$DBprefix."categories";
$r = mysql_query($query1);
$numrow=mysql_num_rows ($r);

for ($i=0;$i<$numrow;$i++)
{
$row = mysql_fetch_row($r);
?>
<input type="radio" name="category[]" value="<? echo $row[1]; ?>"> <? echo $row[1]; ?><br>
<?php
}

this outputs:

c1

(0) sub1.1, sub1.2, sub1.3
 
(0) sub2.1, sub2.2, sub2.3, sub2.4
(0) sub3.1, sub3.2


This is the code from previous page:

$query1 = "Select * from ".$DBprefix."categories";
$r = mysql_query($query1);

$numrow=mysql_num_rows ($r);

for ($i=0;$i<$numrow;$i++)
{
   $field =  mysql_fetch_field($r);
   $tfields[$i] = $field->name;
}
while ($row = mysql_fetch_row($r)) 
{echo "<br><p>";
   $rowAssoc = Array();
   for ($i=0;$i<$numrow;$i++)
   {  
   for ($i=0;$i<1;$i++){
   // print the title of catogory
   $text= ("<a href=\"page2.php?cats=$row[0]\"><strong>$row[0]</strong></a><br>"); 
      }
	   $rowAssoc[$tfields[$i]] = $row[$i];
	
	   foreach(explode(",",substr($rowAssoc[$tfields[$i]],0)) as $vi)
    //print the subcats dividing with a :
 $text= ("$text<a href=\"page2.php?cats=$row[0],$vi\">$vi</a> : "); 
  if ($i=$numrow) {$text= "$text.";}
  echo str_replace(": ."," ", $text);
 }}

I know I need to use foreach, explode and maybe substr bt im not sure which order to put them in.
Any help would be much appreciated. Thx! 


#2 arianhojat

arianhojat
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 11 August 2006 - 02:41 PM

quickly looking @ your question:
so when u arrive to new page you have your catagory ID sent in query string.
and u would do a database lookup for that field with the subcatagories.

$Query = 'SELECT * FROM table WHERE CAT='. $_GET['cat'];
mysql_select_db($db) or die ("Unable to select database!");
$result = mysql_query($Query) or die ("Error in query: $Query. ".mysql_error());
if( $row = mysql_fetch_array($result) )
{
$subcats= $row['SUBCATS'];
}


$subcatsArray = $explode(',' , $subcats); //separates by ','

foreach($subcatsArray as $key => $value )
{

echo "Value at index[".$key."]=". trim( $value ); //trim gets rid of those spaces

}

#3 dharm

dharm
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 12 August 2006 - 12:08 AM

whoohooo got it working!! thx for the info!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users