Jump to content

PHP auto counter


sekowe

Recommended Posts

I want to be able to make a list of number in a colum that changes when a line is added to the middle of it. I have looked for it but cant find anything about it.

 

I will put an axample to explain myself better.

 

#  |  |  |  |  |

------------------

1  | a | a | a | a |

2  | h | h | h | h |

3  | n | n | n | n |

4  | z | z | z | z |

 

Now i want to add a c value between  a and h

 

#  |  |  |  |  |

------------------

1  | a | a | a | a |

2  | h | h | h | h |

                            <----------  3  | c | c | c | c |  I want it to get number 3  automatically and that the rest of the numbers rols one forward.

3  | n | n | n | n |

4  | z | z | z | z |

 

Link to comment
Share on other sites

so if a form inputs the id and the info like n or c or watever

 

Id = 3

info = C | C | C

 

so far you have

 

1 | a | a | a | a |

2 | h | h | h | h |

3 | n | n | n | n |

4 | z | z | z |  z | '

 

and you want to change every thing past it to 1 +

 

<?php
$id = 3;
$query = mysql_query("SELECT * FROM table LIMIT $id-1,18446744073709551615");
while($info = mysql_fetch_array($query)) {
  $update = mysql_query("UPDATE table SET id = $info[id]+1 WHERE id = $info[id]");
}
$insert = mysql_query("INSERT INTO table (id,value) VALUES('$id','C')");
?>

this code is very inpractical becuase if you did have 10,000 database entries it would have to cycle through 9,997 and add 1 to each id.

Link to comment
Share on other sites

I will try out what you just said. But i dont necesarily need it to be on the database.

 

It could also be at the table directly so that it would be independent of the database. Thats actually what i had in mind.

 

It could be something like:

 

<?php

$nc321 = 1;

$nc572 = $nc00001 + 1;

$nc843 = $nc00002 + 1;

$nc443 = $nc00003 + 1;

$nc874 = $nc00004 + 1;

 

echo $nc00001;

echo $nc00002;

echo $nc00003;

echo $nc00004;

echo $nc00005;

?>

 

And then when i want to add one all i have to do is:

 

<?php

$nc321 = 1;

$nc572 = $nc321 + 1;

$nc843 = $nc572 + 1;

                                  <--- add a row    $nc111 = $nc843 + 1;

$nc443 = $nc843 + 1;    (change red to $nc111)

$nc874 = $nc443 + 1;

 

echo $nc321;

echo $nc572;

                          <-----add row echo $nc111;

echo $nc443;

echo $nc874;

?>

 

But i was wondering if there is a code for wich i wouldnt have to write it 1000 times if i have 500 rows. Kind of the effect two tables would have if floating next to each as long as every row has the same size. You add one number to the end of the number table and add a row to the middle of the data table.

 

The code i put is very simple, its because i am just starting. Actualy the first time for me.  :P

Link to comment
Share on other sites

so if a form inputs the id and the info like n or c or watever

 

Id = 3

info = C | C | C

 

so far you have

 

1 | a | a | a | a |

2 | h | h | h | h |

3 | n | n | n | n |

4 | z | z | z |  z | '

 

and you want to change every thing past it to 1 +

 

<?php
$id = 3;
$query = mysql_query("SELECT * FROM table LIMIT $id-1,18446744073709551615");
while($info = mysql_fetch_array($query)) {
  $update = mysql_query("UPDATE table SET id = $info[id]+1 WHERE id = $info[id]");
}
$insert = mysql_query("INSERT INTO table (id,value) VALUES('$id','C')");
?>

this code is very inpractical becuase if you did have 10,000 database entries it would have to cycle through 9,997 and add 1 to each id.

 

This works but its not really what i had in mind. Thanks anyway. Will keep looking for a better option. :D

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.