如何使用PHP回显基于MySQL数据库的动态html片段?(How to echo a dynamic html snippet based on a MySQL databse with PHP?)

编程入门 行业动态 更新时间:2024-10-28 12:27:19
如何使用PHP回显基于MySQL数据库的动态html片段?(How to echo a dynamic html snippet based on a MySQL databse with PHP?)

我正在尝试使用数据库行计数器迭代的for循环创建一个图像库。 为了更清楚:对于表中的每一行,只获取id(主索引)号和服务器的图像链接(不是行中的所有信息)。 使用该信息,使用'src ='内的链接和'alt ='内的id回显HTML图像标记。

这里有两个问题: 1-第一行的id号不为零。 2-我没有关于如何获得总行数以及仅获取这两个信息(id和img源)的线索。

这样,我可以减去总行数减去第一行的id号,并使用它来结束循环。

那么如何基于我的数据库与PHP回应这个动态的html片段呢?

我的代码:

<?php $link = mysqli_connect('localhost','user','pass','db'); $result = mysqli_query($link, "SELECT * FROM `table`"); $rows = mysqli_num_rows($result); /* free result set */ mysqli_free_result($result); $caption = mysqli_query($link, "SELECT "); for($i=0; $i < $rows; $i++) { echo "<img src='$imageURL' alt='$idNumber'>"; } ?>

I'm trying to create an image gallery with a for loop iterated by a counter of database rows. To make it more clear: for each row in the table, get only the id(primary index) number and the image link from the server (not all the info in the row). With that information, echo an HTML image tag with the link inside the 'src=' and the id inside the 'alt='.

Two problems here: 1- the id number of the first row isn't zero. 2- I don't have a clue on how to get the total number of rows and to fetch only those two informations (id and img source).

That way, I could subtract the total number of rows minus the id number of the first row and using it to put an end on the loop.

So how to echo this dynamic html snippet based on my databse with PHP?

My code:

<?php $link = mysqli_connect('localhost','user','pass','db'); $result = mysqli_query($link, "SELECT * FROM `table`"); $rows = mysqli_num_rows($result); /* free result set */ mysqli_free_result($result); $caption = mysqli_query($link, "SELECT "); for($i=0; $i < $rows; $i++) { echo "<img src='$imageURL' alt='$idNumber'>"; } ?>

最满意答案

这是一项非常简单的任务。

首先,我们使用mysqli_query从数据库中获取数据来进行查询。

然后我们使用mysqli_fetch_array来获取一个数组,然后我们可以遍历它并echo每个项目。

之后,我们mysqli_num_rows获取返回的总行数并将其递增1,因此它不为零。

注意:由于您要增加id以避免得到'0',如果您打算将该id用于某些服务器端目的,请不要忘记减去'1'。

$result = mysqli_query($link, "SELECT * FROM `table`"); //query sql $result_array=mysqli_fetch_array($result, MYSQLI_ASSOC);//return array from the query $count = mysqli_num_rows($result); //get numbers of rows received foreach($result as $row){ //do a foreach loop which is really simple echo "<img src='". $row['img_column_name_from_db'] . "' alt='" .$row['id_column_name_from_db'] + 1 . "'>"; //echo data from the array, + 1 to "$row['id_column_name_from_db']" so that 'alt=' doesn't start from '0'. } echo $count;

This is a really easy task.

First we fetch the data from the database using mysqli_query to do the query.

Then we use mysqli_fetch_array to get an array so then we can loop through it and echo each item.

After that, we mysqli_num_rows to get the total number of rows returned and increment it by 1 so it is not zero.

NOTE: Since you are going to increment the id to avoid getting a '0', don't to forget to minus '1' if you intend to use that id for some server-side purpose.

$result = mysqli_query($link, "SELECT * FROM `table`"); //query sql $result_array=mysqli_fetch_array($result, MYSQLI_ASSOC);//return array from the query $count = mysqli_num_rows($result); //get numbers of rows received foreach($result as $row){ //do a foreach loop which is really simple echo "<img src='". $row['img_column_name_from_db'] . "' alt='" .$row['id_column_name_from_db'] + 1 . "'>"; //echo data from the array, + 1 to "$row['id_column_name_from_db']" so that 'alt=' doesn't start from '0'. } echo $count;

更多推荐

本文发布于:2023-08-01 22:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   片段   数据库   动态   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!