开发者

i need a little help on foreach

This is my cats in mysql

cats_id   cats_position   cats_parentid 

1            1>                0
2            1>2>              1
3            3>                0
4            1>2>4>            2

and i am trying to create a navigation like:

index > cars > fiat > punto

from

cat=4&parent=2&position=1>2>4>

I end up getting:

Index carcarcarcar

and my php knowledge is not enough to finish this code. can you help me please.

<?php
$position = trim($_GET['position']);
$pieces = explode(">", $position);

$i=0;
 foreach($pieces as $piece){
 $result = mysql_query("SELECT * FROM cats
 WHERE cats_id='".$pieces[$i]."'");
 while($row = mysql_fetch_array($result))
 {
 $piecesid[$i] = $row['cats_id'];
 $piecesname[$i] = $row['cats_name'];
 $piecesposition[$i] = $row['cats_position'];
 }
 $i++;
 }
 ?>
 <a href="index.php">Index</a>
 <?php $i=0; foreach($pieces as $piece){
 if($i=0)
  $parent=0;
 else
  $parent=$placesid[$i-1];
 echo '<a href="cats.php?cat='.$piecesid[$i].'&parent='.$parent.'&position='.$piecespositi开发者_StackOverflowon[$i].'">'.$piecesname[$i];
}


Your first error is a missing semicolon here:

$i++;

The second bug is a missing dot after $parent in the echo line:

     '&parent='.$parent.'&position='

The third (unexpected end) error will become apparent when you started to indent your code correctly. It's also bad style to omit the curly braces, because that makes it more difficult to find precisely such errors.

And lastly: when posting on Stackoverflow, include the full error message (which always mentions the line number!)


I believe this is what he is looking for:

  1. Create a mysql table with id, parent_id, and name fields.
  2. When a category is a "child" of another, that others parent_id field should be set accordingly, I see you already have something to that effect.
  3. Use this code, setting $cat via $_GET['cat'] or something.

<?php

$cat = mysql_real_escape_string($_GET['cat']);
$res = mysql_query("select id,name,parent_id from categories where id = '$cat'");
$breadcrumb = array();
while ($category = mysql_fetch_object($res) {
    $breadcrumb[] = "<a href=\"?cat={$category->id}\">" . $category->name . "</a>"; 
    if ($category->parent_id != 0) {
        $res = mysql_query("select id,name,parent_id from categories where id = '{$category->parent_id}'");
    }
}
echo join(" > ", array_reverse($breadcrumb));

?>


You have not closed your foreach in the last php section

 <?php $i=0; foreach($pieces as $piece){
 if($i=0)
  $parent=0;
 else
  $parent=$placesid[$i-1];
 echo '<a href="cats.php?cat='.$piecesid[$i].'&parent='.$parent'&position='.$piecesposition[$i].'>'.$piecesname[$i];
  //Missing } here
?>


you're missing a } at the end. Just put a } after the last line like this:

echo '<a href="cats.php?cat='.$piecesid[$i].'&parent='.$parent'&position='.$piecesposition[$i].'>'.$piecesname[$i];
}


By the way, you don't need cats_position in your table. To handle hierarchical data like this you can use a nested set (http://en.wikipedia.org/wiki/Nested_set_model) or just rely on the parent_id. The advantage of this is, that you for example don't need multiple parameters in your get query. Instead of

cat=4&parent=2&position=1>2>4>

you then archieve the same with:

cat=4
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜