Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question How to Loop an output

    Ok

    so I am having a go at creating a database and learnign how to use php to get the info onto a webpage.

    I have a testing server on my local host I am using.

    I have a database with a table in it called hotels and fields (i think 9 I made up)

    Anyway, I am simply trying to pull name (row 2) website (row 7) phone (row 8) from the table and display it.

    I can't seem to get the code right .. I have tried a few different ways .. below is my most recent attempt and I just get a last line error.

    Can someone point me in the right direction so I can get going ?


    EDIT ... I fixed it ... new problem
    Last edited by caymaniac; 01-15-2007 at 10:29 PM. Reason: EDIT a ROW problem .. please help

  • #2
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post

    looping problem ??

    Ok I have fumbled my way through to getting the below outputing page ..

    however I don't understand this loop command very well .. I have had a go but i get an error on the num_rows query .... any ideas what i have done wrong ?

    edit:: i fixed my problem .. replaced the code .. am i looking pretty good for someone that started this stuff today ?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <? 
    // Database information required to connect to database 
    $host="localhost"; 
    $name = "tester"; 
    $pass = "*******"; 
    $dbname = "test"; 
    
    // Connect to Database and select the database to use 
    $dbi = mysql_connect($host, $name,$pass) or 
    die("I cannot connect to the database. Error :" . mysql_error()); 
    mysql_select_db($dbname,$dbi); 
    $query="SELECT * FROM hotels";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    mysql_close();
    ?>
    
    <table border="0" cellspacing="2" cellpadding="2">
    <tr>
    <th><font face="Arial, Helvetica, sans-serif">Name</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Longitude</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Latitude</font></th>
    <th><font face="Arial, Helvetica, sans-serif">City</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Country</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Website</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Phone</font></th>
    <th><font face="Arial, Helvetica, sans-serif">Rating</font></th>
    </tr>
    
    <?
    $i=0;
    while ($i < $num) {
    $name=mysql_result($result,$i,"name");
    $long=mysql_result($result,$i,"long");
    $lat=mysql_result($result,$i,"lat");
    $city=mysql_result($result,$i,"city");
    $country=mysql_result($result,$i,"country");
    $website=mysql_result($result,$i,"website");
    $phone=mysql_result($result,$i,"phone");
    $rating=mysql_result($result,$i,"rating");
    ?>
    
    <tr>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $name; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $long; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $lat; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $city; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $country; ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><a href="<? echo $web; ?>">Website</a></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $phone ?></font></td>
    <td><font face="Arial, Helvetica, sans-serif"><? echo $rating; ?></font></td>
    </tr>
    
    <?
    $i++;
    }
    echo "</table>";
    ?>
    
    </body>
    </html>
    Last edited by caymaniac; 01-14-2007 at 07:58 PM. Reason: i fixed this problem too

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question EDIT a ROW problem .. please help

    OK

    so I am now at the making an edit page ... i went to start with to edit a row .. i think that is all that is needed.
    Unfortunately .. i get the info displayed .. and i can submit it .. but i never get the information actually updated .. can someone take a look and see if they can tell where i have gone wrong ?
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <? 
    //connect to mysql
    // Database information required to connect to database 
    $host="localhost"; 
    $name = "test"; 
    $pass = "******"; 
    $dbname = "testing"; 
    
    // Connect to Database and select the database to use 
    $dbi = mysql_connect($host, $name,$pass) or 
    die("I cannot connect to the database. Error :" . mysql_error()); 
    mysql_select_db($dbname,$dbi); 
    	
    //select which database you want to edit
    mysql_select_db("iunearth"); 
    // make del an action
    if($action=="del")
    	{
    		mysql_query("DELETE FROM hotels WHERE id=$id;");
    	}
    //If cmd has not been initialized
    if(!isset($cmd)) 
    {
       //display all the news
       $result = mysql_query("select * from hotels order by id"); 
       
       //run the while loop that grabs all the news scripts
       while($r=mysql_fetch_array($result)) 
       { 
          //grab the title and the ID of the news
          $name=$r["name"];//take out the title
          $id=$r["id"];//take out the id
         
    	 //make the title a link
          echo "$name - <a href='edit.php?cmd=edit&id=$id'>Edit</a> - <a href='edit.php?action=del&id=$id'>delete</a>";
          echo "<br>";
        }
    }
    ?>
    <?
    if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
    {
       if (!isset($_POST["submit"]))
       {
          $id = $_GET["id"];
          $sql = "SELECT * FROM hotels WHERE id=$id";
          $result = mysql_query($sql);        
          $myrow = mysql_fetch_array($result);
          ?>
    	  
          <form action="edit.php" method="post">
          <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
       
          Hotel:<INPUT TYPE="TEXT" NAME="name" VALUE="<?php echo $myrow["name"] ?>" SIZE=50><br>
          Long:<INPUT TYPE="TEXT" NAME="long" VALUE="<? echo $myrow["long"] ?>"SIZE=14><br>
          Lat:<INPUT TYPE="TEXT" NAME="lat" VALUE="<?php echo $myrow["lat"] ?>" SIZE=14><br>
    	  City:<INPUT TYPE="TEXT" NAME="city" VALUE="<?php echo $myrow["city"] ?>" SIZE=20><br>
          Country:<INPUT TYPE="TEXT" NAME="country" VALUE="<? echo $myrow["country"] ?>"SIZE=20><br>
          Wesbite:<INPUT TYPE="TEXT" NAME="website" VALUE="<?php echo $myrow["website"] ?>" SIZE=100> (no http://)<br>
    	  Phone:<INPUT TYPE="TEXT" NAME="phone" VALUE="<? echo $myrow["phone"] ?>"SIZE=20><br>
          Rating:<INPUT TYPE="INT" NAME="rating" VALUE="<?php echo $myrow["rating"] ?>" SIZE=5><br>
       
          <input type="hidden" name="cmd" value="edit">
       
          <input type="submit" name="submit" value="submit">
       
          </form>
       
       <? } ?>
    <?
       if ($_POST["submit"])
       {
          $id = $_POST["id"]; 
    	  $name = $_POST["name"];
          $long = $_POST["long"];
          $lat = $_POST["lat"];
          $city = $_POST["city"];
          $country = $_POST["country"];
          $website = $_POST["website"];
    	  $phone = $_POST["phone"];
          $rating = $_POST["rating"];
    
          $sql = "UPDATE hotels SET name='$name',long='$long',lat='$lat', city='$city', country='$country', website='$website', phone='$phone', rating='$rating' WHERE id=$id";
          $result = mysql_query($sql);
    
    // Use an IF statement to check and see if $result was true 
    // and the info was updated. 
          if($result) 
          { 
           echo "Update successful"; 
          } 
    // use else to execute the code if  $result was false 
          else 
          { 
          echo "Update failed, please try again"; 
           } 
       } 
    } 
    ?> 
    <p><a href="output.php">BROWSE ALL</a> :: <a href="edit.php">EDIT / DELETE</a> :: <a href="input.php">ADD NEW</a> </p>
    </body>
    </html>
    thanks in advance


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •