Jump to content

[SOLVED] help with arrays


rishiraj

Recommended Posts

My table structure is like

-----------------------------------------

|category | Products |

|----------------------------------------

|category1 | product1 |

|category1 | product2 |

|category1 | product3 |

|category1 | product4 |

|category1 | product5 |

|category1 | product6 |

|category2 | product7 |

|category2 | product8 |

|category2 | product9 |

|category2 | product10 |

|category3 | product11 |

|category3 | product12 |

|category3 | product13 |

|---------------------------------------

 

I want to select all products from table and arrange them in categories. I am using two

 

dimensional array and then foreach loop to do it but not able to suceed. Please help with

 

logic and code.

 

I want to print my products like this.

 

Category1 :

Product1

Product2

Product3

Product4

Product5

Product6

 

Category2:

Product7

Product8

Product9

 

Category3:

Product10

Product11

Product12

Product13

 

Link to comment
Share on other sites

I have made a mess with code,

I am not able to make the logic for it, anyways here is the code

// query code 
$m=-1;
$n=0;
$tempurl="xxxxx";
  	while($row = mysql_fetch_array($result))
  	{	
	if($tempurl!=$row['url'])
	{ $m++; 
	 $n=0;	}
	$url[$m] = $row['url'];
	$tempurl=$url[$m];
	$keyword[$m][$n] =$row['keywords'];
	$n++;
}

// printing code
$o=0;
foreach($url as $url2)
{
	echo "<h3>Keywords for url ".$url2."</h3>";
	foreach($keyword[$o] as $keywordd)
	{
	echo $o." ".$keywordd."\t";
	}
    $o++;
}

 

as you can see from the code i m newbie,

so please help with me logic

Link to comment
Share on other sites

Where is your SQL?

What I would do is select them all out ordered by first the category and then the product.

SELECT * FROM table ORDER BY category, products

 

Then do your loop something like this: (pseudo code, make sure to edit it to work with your code.

<?php
$lastCat = '';
while(//there are more rows){
  $cat = $row['category'];
  if($cat != $lastCat){
     // it's a new category
     print $cat;
   }
   $lastCat = $cat;
   print $row['product'];
}
?>

 

You really should have a seperate categories table and just use the ID to reference what category it is in the products table.

Link to comment
Share on other sites

Lets try this code

<?php
$q = "select category, Products From `Table` Orderby category";
$r = mysql_query($q) or die(mysql_error());
while($row = mysql_fetch_array($r)){
if($temp_cat != $row['category'] || !ISSET($temp_cat)){
	echo "<br/><h1>".$row['category']."</h1>";
}
echo $row['Products']."<br/>";
$temp_cat = $row['category'];
}
?>

It uses recursive logic to test if the next row pulled has the same category as the previous, if not it adds it.

 

Link to comment
Share on other sites

 

dunno i never did php before but i think it would be something like this

 


$m = 0;

while($row = mysql_fetch_array($result)) {

$cata = $row['catagory'];

if($cata == 'category1') {
 $cataproduct[$m][0] =$row['product'];
} else if($cata == 'category2') {
 $cataproduct[$m][1] =$row['product'];
} else if($cata == 'category3') {
 $cataproduct[$m][2] =$row['product'];
}

$m++;

}

 

 

dunno

Link to comment
Share on other sites

yeah it does use recursive logic braand you are out to get me and I don't appreciate it, just because you have no life and thousands of post doesn't mean you can take an attitude of superiority over people

 

Logic is defined by the if statement, the recursive part is the test against the previous row to the current row, recursive isn't defined by a depth, only that it test something to something of similar value that has already been established.

Link to comment
Share on other sites

Where is your SQL?

What I would do is select them all out ordered by first the category and then the product.

SELECT * FROM table ORDER BY category, products

 

Then do your loop something like this: (pseudo code, make sure to edit it to work with your code.

<?php
$lastCat = '';
while(//there are more rows){
  $cat = $row['category'];
  if($cat != $lastCat){
     // it's a new category
     print $cat;
   }
   $lastCat = $cat;
   print $row['product'];
}
?>

 

You really should have a seperate categories table and just use the ID to reference what category it is in the products table.

 

im with jesirose

 

you cant have that in multidimensional array in single query or if you really want then your gonna have additional loop and query which is not good

 

I guess this is solved!!!!

Link to comment
Share on other sites

Thanks a lot everyone,

I thinks everyone's code is similar to the code posted by jesirose, and I am able to understand it.

I am not sure wheather its recursive or not but it worked form me.

Thanks a lot friends.

 

<?php
$lastCat = '';
while(//there are more rows){
  $cat = $row['category'];
  if($cat != $lastCat){
     // it's a new category
     print $cat;
   }
   $lastCat = $cat;
   print $row['product'];
}
?>

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.