Jump to content

Create a pseudo auto increment collumn in database?


TeamCIT

Recommended Posts

Also, I'm not sure if I put your code in the right spot, I'm still having trouble with putting things in the right order. This is what I have so far:

 

if($_POST['submitted'] == true) {

$result = mysql_query("SELECT selected,FROM destinations");
$column_array = mysql_fetch_array($result, MYSQL_NUM);
$High_column_rank = 0;

for($i = 0; $i<14;$i++)
{
$column_current_rank = column_array[$i]; 
if ($column_current_rank > $High_column_rank) {$High_column_rank = $column_current_rank;}
}

  mysql_query("update destinations
             set selected= $High_column_rank 
			where id= $destChose");




while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

}

}else{
		mysql_query("update destinations
    			set selected=1
    			where id={$destChose}");

		$result = mysql_query("SELECT dest_name, dest_addr 
			FROM destinations
			WHERE selected = '1'");			



		while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

		}
		echo "<br />";
		echo "<br />";
		echo "Please select a destination.";
}
}

 

This line is giving me a parse error also, and I'm not sure why. " $column_current_rank = column_array[$i]; "

That piece of code is new to me, I'm not sure how exactly it works.  Does this just put all of the values in the array into a new variable? why is this necessary?

Oh ok, so it looks like i need to add another column. I am just confused about how these two columns are going to interact. Should I keep the "selected" column working with "0"s and "1"s and add an "in_order" column for the other numeric values (1, 2, 3, ect)?

Yes thats correct.... but it's not needed. Two answers do it either way...

 

Answer 1:

But easy answer yes that is correct just keep the old column as it is working, and then do a ranking one. Be sure the in_order column is all intially 0 to start with just as your other one. Display order using that "rank" (in_order) column.

 

Answer 2:

Ok longer answer to ignore if you want but as you are sure to find out that you are updating two sets of data independently that never need to talk to each other.  In fact you can get rid of the whole 0 to 1 column and make the code more efficient / elegant / sensible just stop updating that one period and eliminate it.  WHY? Well since you use the table to display later... u have to use the ranking column to determine what to display first right?  Well if the ranking column has a rank of 0 (its intial value meaning they never ever chose this one) then you not displaying it anyways.... your just going to display it from 1 to what ever according to the ranking column. So that means we never need to check the "0 to 1" column for displaying purposes since all we need is from the "rank"(in_order) column. Why? Since if it was never picked it's rank is still 0... if it's rank is 0 we don't display it.  You already were checking that with the "0 to 1" column... but you don't have to do that anymore. So now since you are not using that to display, it is just a personal error checking column. So once it is working I can leave it or eliminate it and all the code associated with that.

 

This line is giving me a parse error also, and I'm not sure why. " $column_current_rank = column_array[$i]; "

That piece of code is new to me, I'm not sure how exactly it works.  Does this just put all of the values in the array into a new variable? why is this necessary?

First off typo on my part:$column_current_rank = column_array[$i];

needs to be:$column_current_rank = $column_array[$i];

 

Forgot the "$"

 

But to explain the logic...

Well $i is the loop variable starts at 0 goes to 13....

 

column_array is the array holding all the data from that field

meaning:

$column_array[$i] is equal to $column_array[0]    when $i is 0 
$column_array[$i] is equal to $column_array[1]    when $i is 1
etc...

 

$column_current_rank = 

this is really not needed but easier for me to read later is all I did that for.....

   $column_current_rank = column_array[$i];
   if ($column_current_rank > $High_column_rank) {$High_column_rank = $column_current_rank;}

could be rewritten to this (but does the same thing):

   if ($column_array[$i] > $High_column_rank) {$High_column_rank = $column_array[$i];}

Ok thank you for the explanation I'm beginning to understand more what is happening.  I created the (in order) column in my database, now I know the next step is to add a MySQL query to update this column but I am not sure where to put this code.  Here is what I have now:

if($_POST['submitted'] == true) {

$result = mysql_query("SELECT selected,FROM destinations");
$column_array = mysql_fetch_array($result, MYSQL_NUM);
$High_column_rank = 0;

for($i = 0; $i<14;$i++)
{
$column_current_rank = $column_array[$i]; 
if ($column_current_rank > $High_column_rank) {$High_column_rank = $column_current_rank;}
}

  mysql_query("update destinations
             set selected= $High_column_rank 
			where id= $destChose");




while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

}

}else{
		mysql_query("update destinations
    			set selected=1
    			where id={$destChose}");

		$result = mysql_query("SELECT dest_name, dest_addr 
			FROM destinations
			WHERE selected = '1'");			



		while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

		}
		echo "<br />";
		echo "<br />";
		echo "Please select a destination.";
}

 

Should I replace this

mysql_query("update destinations
             set selected= $High_column_rank 
			where id= $destChose");

 

With something like this?

mysql_query("update destinations
             set in_order= $High_column_rank 
			where id= $destChose");

Should I replace this

Code: [select]

mysql_query("update destinations

            set selected= $High_column_rank

            where id= $destChose");

With something like this?

Code: [select]

mysql_query("update destinations

            set in_order= $High_column_rank

            where id= $destChose");

 

 

Yes, cause otherwise the "0 to 1" column named selected is being updated in the first one.

The second one is updating the "ranking" column named in_order.  Otherwise what I am seeing looks good so far. If it comes out with odd output then you know you got something crossed. Let me know, got me tangled up in seeing this work now haha

haha good i'm glad you're as interested in getting this to work as i am.  Everything appears ok until I try to submit a destination, i get these warning errors:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\...\createTour_inorderRedone.php on line 16

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\...\createTour_inorderRedone.php on line 32

 

These lines are:

 

$column_array = mysql_fetch_array($result, MYSQL_NUM);

 

and

 

while ($row = mysql_fetch_array($result)) {

 

This is all of the code I'm using in this part just to make things easier to see:

 

if($_POST['submitted'] == true) {

$result = mysql_query("SELECT selected,FROM destinations");
$column_array = mysql_fetch_array($result, MYSQL_NUM);
$High_column_rank = 0;

for($i = 0; $i<14;$i++)
{
$column_current_rank = $column_array[$i]; 
if ($column_current_rank > $High_column_rank) {$High_column_rank = $column_current_rank;}
}

  mysql_query("update destinations
             set in_order= $High_column_rank
            where id= $destChose");




while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

}

}else{
		mysql_query("update destinations
    			set selected=1
    			where id={$destChose}");

		$result = mysql_query("SELECT dest_name, dest_addr 
			FROM destinations
			WHERE selected = '1'");			



		while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

		}
		echo "<br />";
		echo "<br />";
		echo "Please select a destination.";
}

I believe that is a redundant error... using the result to store twice and sql is not liking it at all change this first to see if it works....

if($_POST['submitted'] == true) {
   
  // $result = mysql_query("SELECT selected,FROM destinations");
   $Col_result = mysql_query("SELECT selected,FROM destinations");
  // $column_array = mysql_fetch_array($result, MYSQL_NUM);
   $column_array = mysql_fetch_array($Col_result, MYSQL_NUM);
   $High_column_rank = 0;

 

If it still pukes I'd keep that changed anyways since you want the $result for later at anyrate. Otherwise also try closing the database after this line

 

  mysql_query("update destinations
             set in_order= $High_column_rank
            where id= $destChose");
         
   CLOSE THE DATABASE HERE
THEN REOPEN IT HERE MAY HELP TO RESET POINTERS AND WHATNOT

   
   while ($row = mysql_fetch_array($result)) {
   
            echo "<li>";
            echo $row["dest_name"];
            echo "</li>";

 

then reopenning it for the next line.

I tried these things, still no luck.  I think it might have something to do with the fact that my selected column is no longer being updated, is this a problem? I changed a few things in my code too, I'll put stars next to the lines i changed but I might forget to mark a few.

 

Also I am now getting the same two warnings, but for different lines in my code :

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\www\adibuono\BPA\createTour_inorderRedone.php on line 18

$column_array = mysql_fetch_array($Col_result, MYSQL_NUM);

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\www\adibuono\BPA\createTour_inorderRedone.php on line 35

while ($row = mysql_fetch_array($result)) {

 

$link = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql");

//Select Database
mysql_select_db($dbname);

if($_POST['submitted'] == true) {
   
  // $result = mysql_query("SELECT selected,FROM destinations");
   $Col_result = mysql_query("SELECT selected,FROM destinations"); **
  // $column_array = mysql_fetch_array($result, MYSQL_NUM);
   $column_array = mysql_fetch_array($Col_result, MYSQL_NUM); **
   $High_column_rank = 0;

for($i = 0; $i<14;$i++)
{
$column_current_rank = $column_array[$i]; 
if ($column_current_rank > $High_column_rank) {$High_column_rank = $column_current_rank;}
}

  mysql_query("update destinations
             set in_order= $High_column_rank
            where id= $destChose");

mysql_close($link); **

mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql"); **

while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

}

}else{
		mysql_query("update destinations
    			set in_order= $High_column_rank
    			where id={$destChose}");

		$result = mysql_query("SELECT dest_name, dest_addr 
			FROM destinations
			WHERE in_order >='1'");			**



		while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

		}
		echo "<br />";
		echo "<br />";
		echo "Please select a destination.";
}

mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql"); **
////////////I think $results has to be something here
/// like this:   $result = mysql_query("SELECT CHANGEMETOFIELDUNEEDBELOWTHIS,FROM destinations");
   while ($row = mysql_fetch_array($result)) {

 

Second error is the fact that $result is not point to anything

 

First error maybe it just doesn;t like the MYSQL_NUM part... weird.

try it without that and see what happens I guess.

 

$column_array = mysql_fetch_array($Col_result);

 

Second error is the fact that $result is not point to anything

 

First error maybe it just doesn;t like the MYSQL_NUM part... weird.

try it without that and see what happens I guess.

 

$column_array = mysql_fetch_array($Col_result);

 

Ok, I took out the NUM part and added a $result value before the While statement and i'm still having warnings on those lines.  Any ideas?:

 

if($_POST['submitted'] == true) {
   
  // $result = mysql_query("SELECT selected,FROM destinations");
   $Col_result = mysql_query("SELECT selected,FROM destinations");
  // $column_array = mysql_fetch_array($result, MYSQL_NUM);
   $column_array = mysql_fetch_array($Col_result);
   $High_column_rank = 0;

for($i = 0; $i<14;$i++)
{
$column_current_rank = $column_array[$i]; 
if ($column_current_rank > $High_column_rank) {$High_column_rank = $column_current_rank;}
}

   mysql_query("update destinations
             set in_order= $High_column_rank
            where id= $destChose");

mysql_close($link);

mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql");
$result = mysql_query("SELECT dest_name, dest_addr 
			FROM destinations
			WHERE in_order >='1'");	

while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

}

}else{
		mysql_query("update destinations
    			set in_order= $High_column_rank
    			where id={$destChose}");

		$result = mysql_query("SELECT dest_name, dest_addr 
			FROM destinations
			WHERE in_order >='1'");			



		while ($row = mysql_fetch_array($result)) {

			echo "<li>";
			echo $row["dest_name"];
			echo "</li>";

		}
		echo "<br />";
		echo "<br />";
		echo "Please select a destination.";
}

if($_POST['reset'] == true) {
mysql_query("update destinations
    			set selected=0
    			where selected='1'");
}

if($_POST['linkgen'] == true) {
$result = mysql_query("SELECT dest_addr 
			FROM destinations
			WHERE selected = '1'");			

$counter = mysql_num_rows($result); 


echo "<a href=\"";

while ($row = mysql_fetch_array($result)) {
		$address[] = $row["dest_addr"];
}


		switch($counter) {
	case 1:
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		break;
	case 2:
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		break;		
	case 3:
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		break;
	case "4":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		break;
	case "5":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		break;
	case "6":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		echo "+to:";
		echo $address[5];
		break;
	case "7":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		echo "+to:";
		echo $address[5];
		echo "+to:";
		echo $address[6];
		break;
	case "8":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		echo "+to:";
		echo $address[5];
		echo "+to:";
		echo $address[6];
		echo "+to:";
		echo $address[7];
		break;
	case "9":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		echo "+to:";
		echo $address[5];
		echo "+to:";
		echo $address[6];
		echo "+to:";
		echo $address[7];
		echo "+to:";
		echo $address[8];
		break;
	case "10":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		echo "+to:";
		echo $address[5];
		echo "+to:";
		echo $address[6];
		echo "+to:";
		echo $address[7];
		echo "+to:";
		echo $address[8];
		echo "+to:";
		echo $address[9];
		break;
	case "11":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		echo "+to:";
		echo $address[5];
		echo "+to:";
		echo $address[6];
		echo "+to:";
		echo $address[7];
		echo "+to:";
		echo $address[8];
		echo "+to:";
		echo $address[9];
		echo "+to:";
		echo $address[10];
		break;
	case "12":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		echo "+to:";
		echo $address[5];
		echo "+to:";
		echo $address[6];
		echo "+to:";
		echo $address[7];
		echo "+to:";
		echo $address[8];
		echo "+to:";
		echo $address[9];
		echo "+to:";
		echo $address[10];
		echo "+to:";
		echo $address[11];
		break;
	case "13":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		echo "+to:";
		echo $address[5];
		echo "+to:";
		echo $address[6];
		echo "+to:";
		echo $address[7];
		echo "+to:";
		echo $address[8];
		echo "+to:";
		echo $address[9];
		echo "+to:";
		echo $address[10];
		echo "+to:";
		echo $address[11];
		echo "+to:";
		echo $address[12];
		break;
	case "14":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $address[0];
		echo "&daddr=";
		echo $address[1];
		echo "+to:";
		echo $address[2];
		echo "+to:";
		echo $address[3];
		echo "+to:";
		echo $address[4];
		echo "+to:";
		echo $address[5];
		echo "+to:";
		echo $address[6];
		echo "+to:";
		echo $address[7];
		echo "+to:";
		echo $address[8];
		echo "+to:";
		echo $address[9];
		echo "+to:";
		echo $address[10];
		echo "+to:";
		echo $address[11];
		echo "+to:";
		echo $address[12];
		echo "+to:";
		echo $address[13];
		break;
	default:
		break;
		}

echo "&pw=2";
echo "\">" . "Directions" . "</a>";
}
?>


<html>
<head>
<title>Create a Tour</title>
</head>

<body>
<br />
<br />
<form name="tourCreation" action="createTour_inorderRedone.php" method="post">
Please complete this form to create your tour:
<br />
<br />
Destinations:<br />
<select name="destList" size="10">
  <option value="1">National Gallery of Art</option>
  <option value="2">National Archives & Records Administration</option>
  <option value="3">National Law Enforcement Officers Memorial</option>
  <option value="4">Kenilworth Aquatic Gardens</option>
  <option value="5">White House Park</option>
  <option value="6">Washington Photo Safari</option>
  <option value="7">Federal Bureau of Investigation</option>
  <option value="8">Franciscan Monastery - Commissariat of the Holy Land in America</option>
  <option value="9">National Portrait Gallery Special Events Department</option>
  <option value="10">Rock Creek Park</option>
  <option value="11">Anacostia Community Museum</option>
  <option value="12">National Shrine of the Immaculate Conception </option>
  <option value="13">U.S. National Arboretum</option>
  <option value="14">Union Station</option>
   <option value="23">Tommy J</option>
</select>
<br />
<br />
<br />
<input type="submit" name="submitted" value="Select Destination" />
<input type="submit" name="reset" value="Reset" />
<input type="submit" name="linkgen" value="Get Link" />
</form>

<br />
<br />
</body>
</html>

I'm a bit rusty on the mysql calls... ok it's starting to get beyond me here. But I thought that  would work.

 

Ok lets mark this forum thread solved... since we got what u wanted for solving the second column issue.

We've gone into a new realm of what is wrong with my MY_SQL stuff realm.

Let;s start a new forum thread post with that and see if someone else can say what is wrong with the syntax I gave you. I don't have access here to one to test it myself... but if changing those lines do nothing. Then maybe something before each line. I am at a lost unfortunately now without being able to test it.  :-\

 

Start new forum with this subject:  Help MY_SQL issues and explain the issue and give code using. Someone may rip me a new one for doing something wrong I think.

I'm a bit rusty on the mysql calls... ok it's starting to get beyond me here. But I thought that  would work.

 

Ok lets mark this forum thread solved... since we got what u wanted for solving the second column issue.

We've gone into a new realm of what is wrong with my MY_SQL stuff realm.

Let;s start a new forum thread post with that and see if someone else can say what is wrong with the syntax I gave you. I don't have access here to one to test it myself... but if changing those lines do nothing. Then maybe something before each line. I am at a lost unfortunately now without being able to test it.  :-\

 

Start new forum with this subject:  Help MY_SQL issues and explain the issue and give code using. Someone may rip me a new one for doing something wrong I think.

 

it's alright you were a huge help, thank you very much i appreciate it.

No Problem, but I do suggest starting a new thread too just to see if can get any new blood to look at this. Some very good people here to help, trying to get them on board to help.

SO good luck. Hopefully soemone has an idea.

No Problem, but I do suggest starting a new thread too just to see if can get any new blood to look at this. Some very good people here to help, trying to get them on board to help.

SO good luck. Hopefully soemone has an idea.

 

Thanks I'll need the luck haha. Started a new thread here http://www.phpfreaks.com/forums/index.php/topic,286569.0.html

Just in case you find someone to help or are just curious to see how it works out.

Thanks again!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.