Jump to content

Archived

This topic is now archived and is closed to further replies.

dharm

Foreach Explode array

Recommended Posts

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:

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

this outputs:

[b]c1[/b]

(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:

[code]$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);
}}[/code]

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! 

Share this post


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

}

Share this post


Link to post
Share on other sites

×

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.