1

我从mysql中选择了一些行

我想在单词之间加上下划线

像这样 :

<?php
$query='SELECT * FROM subcates WHERE maincates_id=1';
$result=mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
    echo "<li><a href='".$row['name'].".php?c=".$row['id']."&gov=1&h=1'>".$row['name']."</a></li>";    
}
?>

我想在选择名称时给我这个:

database ==> first second

我想要它什么时候selected ==> first_second

谢谢

4

2 回答 2

2

尝试:

//Considering its the $row['name'] that you want in first_second format
while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
  $row['name'] = str_replace(' ', '_', $row['name']);
  echo "<li><a href='".$row['name'].".php?c=".$row['id']."&gov=1&h=1'>".$row['name']."</a></li>";

}

编辑:

  $query='SELECT * FROM subcates WHERE maincates_id=1';
  $result = mysql_query($query); 
  while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
  if( current($result) === 'First second' ) {
    $new_name = str_replace(' ', '_', $row['name']);
   echo "<li><a href='".$new_name.".php?c=".$row['id']."&gov=1&h=1'>".$new_name."  </a></li>";
 }
  else        
     echo "<li><a href='".$row['name'].".php?=".$row['id']."&gov=1&h=1'>".$row['name']."  </a></li>";    
 }
于 2012-11-06T15:14:54.243 回答
2

如果您只想用下划线替换空格,请运行

str_replace(" ","_",$row["name"]). 请参阅http://php.net/manual/en/function.str-replace.php

于 2012-11-06T15:16:40.403 回答