Jump to content

Changing php display


FUNKAM35

Recommended Posts

Hi, say I want to change something on my php page in mysql databse. For example

I want to change where it is cat in the database to display as dog on the page what is the code I use?

Hope this makes sense!

Also I need to alter several fields so change cat to dog and pig to cow how would I do this?

Thank you all help much appreciated

Link to comment
Share on other sites

@Also I need to alter several fields so change cat to dog and pig to cow how would I do this?  -> you do a mysql_query

example :

mysql_query("UPDATE table SET field='cow' WHERE field='pig' ") 

that changes all the cow to pig..

Link to comment
Share on other sites

<?php
if($con = mysql_connect('server', 'username', 'password')) 
 mysql_select_db('db_name')
   or die('MySQL Error: ' . mysql_error());

$sql = mysql_query("SELECT * FROM `db_name` . `table_name`", $con)
          or die('MySQL Error: ' . mysql_error());

if(mysql_num_rows($sql) > 0) {
 while($row = mysql_fetch_array($sql)) {
   if($row['field_name'] == 'cat')
     print 'dog';
 }
}

// Or you can use switch() as zenag suggested.

if(mysql_num_rows($sql) > 0) {
  switch($row['field_name']) {
    case 'cat':
      print 'dog';
      break;
    case 'dog':
      print 'cat';
      break;
    default: 
      print 'cat & dog';
      break;
  }
}

mysql_close($con);
?>

Link to comment
Share on other sites

u can use switch..
(eg:)
$row=$row["field"];
switch ($row) {
case 'pig':
         $var="cow";
    
    break;
case 'dog':
    $var="pig";
    break;



}

 

Thank you can you please tell me which do I alter do I insert mysql field into $row but what about $var?

Link to comment
Share on other sites


if(mysql_num_rows($sql) > 0) {
  while($row = mysql_fetch_array($sql)) {

$row=$row["field"];
switch ($row) {
case 'pig':
         $var="cow";
    
    break;
case 'dog':
    $var="pig";
    break;

}
echo $var;
}

$var is nothing but assigning variable for ...$row["field"]

Link to comment
Share on other sites

Hi I have got it to work for the first item but because of the formatiing it messes the display up because the <p> formatting finishes after the switched item.

This is what I have got

 

 

echo"<p class = \"display\"><img src=\"animals/$animal.jpg\" width=\"27\" height=\"21\" alt=\"$animal\"  />$animal<br />";

switch ($animal){

case "Dog":

echo"TEST</p>";

break;

}

 

 

WHAT i need is to fit the switch into the normal code:

echo"<p class = \"display\"><img src=\"animals/$animal.jpg\" width=\"27\" height=\"21\" alt=\"$animal\"  />$animal<br />";

 

but when I try and insert it it goes blank

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.