回应使用MySQL从数据库中提取数据的按钮
问题描述:
I have an issue with either quotation marks or syntax, I'm not entirely sure.
Here's the code:
<div class="pagination btn-group" id="items">
<? $sql = "SELECT itemname FROM items ORDER BY itemname ASC";
$result = mysql_query($sql);
$id = $row['id'];
$itemname = $row['itemname'];
$price = $row['price'];
while ($row = mysql_fetch_array($result))
{
echo "<button class='btn btn-medium highlight-color-0' id=$id itemname=$itemname price=$price><div class='btn-image gears'></div><span class='btn-text'>$itemname</span>";
}
echo "</button>"; ?>
</div>
I then call to this in jQuery using this code:
$('.pagination btn-group').click(function()
{
id = $(this).attr('id');
name = $(this).attr('itemname');
price = $(this).attr('price');
});
However, I seem to be getting undefined results? Any help would be appreciated
我的引号或 语法都有问题,我不完全确定。 p>
以下是代码: p>
&lt; div class =“pagination btn-group”id =“items”&gt;
&lt;? $ sql =“SELECT itemname FROM items ORDER BY itemname ASC”;
$ result = mysql_query($ sql);
$ id = $ row ['id'];
$ itemname = $ row ['itemname '];
$ price = $ row ['price'];
while($ row = mysql_fetch_array($ result))
{
echo“&lt; button class ='btn btn-medium highlight- color-0'id = $ id itemname = $ itemname price = $ price&gt;&lt; div class ='btn-image gears'&gt;&lt; / div&gt;&lt; span class ='btn-text'&gt; $ itemname&lt; / span&gt;“;
}
echo”&lt; / button&gt;“; ?&gt;
&lt; / div&gt;
code> pre>
然后我使用以下代码在jQuery中调用它: p>
$('。pagination btn-group')。click(function()
{
id = $(this).attr('id');
name = $(this).attr('itemname ');
price = $(this).attr('price');
});
code> pre>
但是,我似乎得到了未定义的结果 ? 任何帮助将不胜感激 p>
div>
答
you should put the below lines inside while loop
$id = $row['id'];
$itemname = $row['itemname'];
$price = $row['price'];
should be like
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$itemname = $row['itemname'];
$price = $row['price'];
echo "<button class='btn btn-medium highlight-color-0' id=$id itemname=$itemname price=$price><div class='btn-image gears'></div><span class='btn-text'>$itemname</span>";
}