سؤال

i have selected some rows from mysql

and i want put underscore between the words

like this :

<?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>";    
}
?>

i want when select the name give me this :

in database ==> first second

i want it when selected ==> first_second

thx

هل كانت مفيدة؟

المحلول

Try:

//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>";

}

Edit:

  $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>";    
 }

نصائح أخرى

If all you want to do is to replace the space with an underscore, run

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top