Gain an index of the specified row

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
taubi19
Forum Newbie
Posts: 7
Joined: Wed Mar 03, 2010 9:53 am
Location: Oplotnica, Slovenia

Gain an index of the specified row

Post by taubi19 »

Hi!

I've got the folowing problem. I am putting together a web page where the ovner has the privilege to change the page content within an special interface. For that purpose I had to use a db. And now I want to make the owner the possibility to insert some data just where he wants it. So I want to make it possible to insert some content at any position in the table. But firstly I dont know how to get an index of the place he want's to insert data in, and secondly how do I insert some data to the specified index.

This is my code:

Code: Select all

 
$sql="SELECT content FROM $table_name";
$data=mysql_query($sql) or die(mysql_error());
                    
while($row=mysql_fetch_array($data)){  //here I display the data to the page 
    echo $row['content'];
    echo '  <hr size="2px" align="center" width="90%px"/>
            <form method="post" action="admin_script.php name="forma1"> 
                                 //and here are the buttons for deleting, editing or creating a new post/entry to the table 
                <input type="submit" name="edit" value="Uredi"/>
                <input type="submit" name="delete" value="Izbrisi vnos"/>
                <input type="submit" name="new" value="Nov vnos"/>
            </form>
            ';      
                    echo '<hr size="5" width="98%" noshade/><br>';
                }
 
I think it would be great if I could save the information about the position of the data near every output, but don't know how.
I would appreciate any help!! Thank you!!


Oh, and sorry for my not so good english :S
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Gain an index of the specified row

Post by tr0gd0rr »

Database table rows do not implicitly have an order or an index.

Many times a query without an ORDER BY shows the rows in a different order than they were inserted. In theory, a query without ORDER BY could return results in different order from one moment to the next.

You need a column with an index or id to uniquely identify a row. Making a MySQL column AUTOINCREMENT and PRIMARY KEY will take care of that.

If you want a user to be able to choose an order in the table, use a sort column column (e.g. `display_sequence`) that is an int or float. In your SELECT query ORDER BY `display_sequence`.
Post Reply