45808
45808

Reputation: 376

Database data in PHP array

I have a table in phpmyadmin that stores an 'id' (auto inc), 'title' and a 'date'.

I have a webpage where I display the 10 latest items (I simply order by ID). On that page I print the title and the date. My wish is to also display the number of the posted item, so the first posted item is 1, the second is 2, etc. I cannot simply print the ID from the database because if I delete a row, the numbers aren't straight anymore.

My idea was to put all the data in an array but I have no clue what the best way to do this is and how I could print that item number. So for example when I want to display the 54th item I can easily print $items[54][id] or something and it will show me the number 54 and to display the title I print $items[54][title].

I don't know if there are simpler methods, plus arrays always start at 0, but my items must start at 1.

Besides this page that shows the 10 latest items, there is another page where it gets the title of the item out of the URL. How will I be able to search the title in the array and display the data the same way but only for that requested title?

Thanks in advance!

Upvotes: 2

Views: 282

Answers (4)

Headshota
Headshota

Reputation: 21449

"SELECT COUNT(id) as cnt FROM mytable";

you can select the count of all database entries.

and then assign it to your iterator

$i = $row['cnt']; // this will hold the ammount of records e.g. 21

// other query

while($row = mysql_fetch_assoc($result)) {
echo $i;

$i--; // this will decrement on every iteration 21, 20 , 19, and so on.
}

Upvotes: 1

markus
markus

Reputation: 40675

You should learn PHP before starting to program in PHP ;) Read and work through the PHP manual and some tutorials!

As to your question it is a simple loop you want to do. One way of doing it as an example.

Fetch the 10 last items from the database in any way you like, following some code, partly pseudo-code.

$markup = '';

for ($i=1; $i<=count($items); $i++)
{
    $markup .= 'Item ' . $i . ': ' . $items['date'] . ' - ' . $items['title'];
    $markup .= '<a href="/path/to/postpage/id/' . $items['id'] . '">read more</a>';
    $markup .= PHP_EOL;
}

echo $markup;

Upvotes: 1

Zeta Two
Zeta Two

Reputation: 1806

First off. I would add a timestamp field to the database and order by that instead as it feels overall more reliable and gives you additional details which may prove handy later.

To create the multidimensional array I would do something like:

$result = mysql_query(...);
$items = array();
while($item = mysql_fetch_assoc($result)) {
$items[] = $item;
}

Now $items[12] for example would give you item number 13 (since it's 0-indexed).

Lastly, to select only the item with a specific title I would use a query which included a WHERE clause, like

"SELECT ... FROM ... WHERE title = '".$title."'"

It's very important to sanitize this variable before using it in the query though.

You can read more about MySQL on a lot of places. A quick googling gave me this: http://www.tutorialspoint.com/mysql/index.htm

Upvotes: 1

dee-see
dee-see

Reputation: 24078

I don't know how you print out your data exactly, but I assume there is a loop in there. Simply set a counter that increments by one at every row and print its value.

As for the title search, you'll have to run another query with a WHERE title = '$title' condition, but beware of SQL injection.

Upvotes: 0

Related Questions