在PHP数组数据库中的数据 [英] Database data in PHP array

查看:133
本文介绍了在PHP数组数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在phpMyAdmin一个表,用于存储的ID(汽车公司),标题和日期。

我有一个网页,我展示10个最新的项目(我简单地通过ID顺序)。
在该页面中我打印的标题和日期。我的愿望是还可以显示相关商品的数量,所以第一个发布产品1,第二个是2,等等。我不能简单地从数据库打印ID,因为如果我删除一行,数量也不会直了。

我的想法是把所有的数据在一个数组,但我不知道该怎么做到这一点的最好办法是,我怎么能打印的项目编号。因此,例如,当我想要显示的第54项,我可以轻松打印$项目[54] [ID]或东西,它会告诉我的号码54和显示标题我打印$项目[54] [标题]。

我不知道是否有简单的方法,再加上数组总是从0开始,但我的项目必须从1开始。

除了这个页面,显示了10个最新的项目,有它得到的产品的标题出来的URL的另一页。我如何能够搜索到阵列中的标题和显示数据的方式相同,但仅适用于请求标题?

在此先感谢!


解决方案

 SELECT COUNT(ID)为CNT FROM MYTABLE

您可以选择所有数据库条目的数量。

,然后将其分配给你的迭代

  $ I = $行['CNT']; //这将持有的记录ammount的例如21//其他查询而($行= mysql_fetch_assoc($结果)){
回声$ I;$我 - ; //这将减小在每次迭代21,20,19,等等。
}

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!

解决方案

"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.
}

这篇关于在PHP数组数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆