Jump to content

Anyone know why this isn't ordering them


liam1412

Recommended Posts

Will this bit do.  Its my first scropt so go easy. lol

[code]<?php 
$last_cars = "SELECT * FROM last_cars WHERE userid='$userid' ORDER BY car_id ASC";
$last_query = mysql_query($last_cars);
$no_cars = mysql_num_rows($last_query);
?>

<div class="last5">
<table width="396" cellpadding="6" cellspacing="1">
<tr>
<td class="header2" colspan="4" bgcolor="#565656">Previous Cars -
<?php if($username == $_SESSION['username']){echo ?><a href="edit_last5.php?userid=<?php echo $userid?>">Change</a><?php } ?></td>
</tr>
<tr>
<td class="grey" colspan="4" width="400" bgcolor="#6d6d6d">Love em or hate. Your last 5 cars. BE HONEST!!!</td>
</tr>
<tr>
<td class="header3" width="50" bgcolor="#6d6d6d">Year</td>
<td class="header3" width="150" bgcolor="#6d6d6d">Make</td>
<td class="header3" width="150" bgcolor="#6d6d6d">Model</td>
<td class="header3" width="45" bgcolor="#6d6d6d">Engine</td>
</tr>
<?php
while($car_result = mysql_fetch_array($last_query)){
$plate = $car_result['plate'];
$make = $car_result['make'];
$model = $car_result['model'];
$engine = $car_result['engine'];
?>
<tr>
<td class="grey" width="50" bgcolor="#6d6d6d"><?php echo $plate; ?></td>
<td class="grey" width="150" bgcolor="#6d6d6d"><?php echo $make; ?></td>
<td class="grey" width="150" bgcolor="#6d6d6d"><?php echo $model; ?></td>
<td class="grey" width="45" bgcolor="#6d6d6d"><?php echo $engine; ?></td>
</tr>
<?php
}
?>
</table>[/code]
Link to comment
Share on other sites

I also have a query that deletes the oldest one if there are more than 5.  Just in case you need it

[code]<?php
session_start();
$userid = $_GET['userid'];
$plate = $_POST['plate'];
$make = $_POST['make'];
$model = $_POST['model'];
$engine = $_POST['engine'];

$host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'klubdeutsch';
mysql_connect("$host" , "$db_username" , "$db_password")or die("Cannot Connect to Server");
mysql_select_db("$db_name")or die("Cannot connect to Databse");

$last5 = "SELECT * FROM last_cars WHERE userid='$userid'";
$last5_query = mysql_query($last5);
$total = mysql_num_rows($last5_query);

if($total == 5){
$oldest_car = "SELECT * FROM last_cars WHERE userid='$userid' ORDER BY car_id ASC LIMIT 1";
$query = mysql_query($oldest_car);
$result = mysql_fetch_array($query);
$car_id = $result['car_id'];

$delete_oldest = "DELETE FROM last_cars WHERE car_id='$car_id'";
$query = mysql_query($delete_oldest)or die('Dint Work');
}

$add_new = ("INSERT INTO last_cars (userid, plate, make, model, engine)
values('$userid' , '$plate' , '$make' , '$model' , '$engine')")or die('Dint Work 2');
$add_car = mysql_query($add_new);

if($add_car){

header("location:profile2.php?userid=$userid");

}else{

echo broke;
}

?>[/code]
Link to comment
Share on other sites

I have a form that users can add a car to the list on there profile.  if there is more then 5 it deletes the last one and replaces it with the new one.  I want that one to remain on top but its ending up wherever it feels like. The first bit of code is the display of the list. the second bit adds to the list and deletws if neccessary. 
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.