[英]I want to display items from mySQL database in grid form using php but i'm getting this
I want to display records from my database in div and in a grid format using php. 我想使用php以div和网格格式显示数据库中的记录。 I want to set it to display 3 items in a row and then create another row and then display another 3 items continuously until the set limit but unfortunately only one item is showing.
我想将其设置为连续显示3个项目,然后创建另一行,然后连续显示另外3个项目,直到达到设置的限制,但是不幸的是,仅显示一个项目。 Seems there is something wrong with the php code somewhere which i don't know.
似乎我不知道的地方的php代码有问题。 Please someone should help me figure it out.
请有人帮助我解决。 The code is below
代码如下
<?php
<!--for image upload-->
session_start();
$_SESSION['message']="";
$mysqli = new mysqli('localhost', 'root', '','auction');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$item_name = $mysqli->real_escape_string($_POST['item_name']);
$item_description = $mysqli->real_escape_string($_POST['item_description']);
$item_image_path = $mysqli-
>real_escape_string('Images/item_img/'.$_FILES['item_image']['name']);
<!--make sure file is of image type-->
if (preg_match("!image!", $_FILES['item_image']['type'])) {
if (copy($_FILES['item_image']['tmp_name'], $item_image_path)) {
$_SESSION['item_name'] = $item_name;
$_SESSION['item_description'] = $item_description;
$_SESSION['item_image'] = $item_image_path;
$sql = "INSERT INTO items (item_name, item_image, item_description)
VALUES('$item_name',
'$item_image_path','$item_description')";
if ($mysqli->query($sql) === true) {
$_SESSION['message'] = "Item Upload Successful!";
}
else {
$_SESSION['message'] = "file upload failed";
}
}
else{
$_SESSION['message'] = "file copying failed";
}
}
else {
$_SESSION['message'] = "please upload gif, jpg, png";
}
}
?>
<?php
$result = $mysqli->query("SELECT * FROM items ORDER BY rand() LIMIT 10")
or die($mysqli->error);
?>
<html>
<head>
<title>Upload item</title>
<link rel="StyleSheet" href="Bootstrap/css/bootstrap.main.css">
<link rel="StyleSheet" href="Bootstrap/css/bootstrap.min.css">
<link rel="StyleSheet" href="style.css">
<!--for countdown timer-->
<script>
var seconds = 60*60;
function secondPassed() {
var minutes = Math.round((seconds - 30)/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = minutes + ":" +
remainingSeconds;
if (seconds == 0) {
clearInterval(countdownTimer);
document.getElementById('countdown').innerHTML = "Buzz Buzz";
} else {
seconds--;
}
}
var countdownTimer = setInterval('secondPassed()', 1000);
</script>
</head>
<body>
<div>
<!--to display records from database-->
<div class = "row c-head mar-pad">
<?php
$A=0;
while ($auction = $result->fetch_assoc()):?>
<?php
if ($A%3==0):?>
<div class = "">
<h4><?=$auction['item_name']?></h4>
<img src='<?=$auction['item_image']?>' class='img-
responsive'>
<span id="countdown" class="timer">how</span>
<button class ="c-button" name ='bid'>Bid Now!</button>
</div>
<?php $A++;?>
<?php endif;?>
<?php endwhile;?>
</div>
<!--for file upload form-->
<form class="form-horizontal" role="form"
action="auction_upload.php" method="POST" enctype="multipart/form-data">
<h1><?=$_SESSION['message']?></h1>
<div class=" form-group">
<label class="control-label col-sm-2">Item Name:</label>
<div class="col-sm-8">
<INPUT type="text" class="form-control" name="item_name"
required/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Item Image:</label>
<div class="col-sm-8">
<INPUT type="file" class="form-control" name="item_image"
accept="image/*" required/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Item Description:</label>
<div class="col-sm-8">
<textarea class="form-control" name="item_description" required>
</textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<button type="submit" class="btn btn-default"
name="upload">Upload</button>
</div>
</div>
</form>
</div>
</body>
</html>
I'm not sure how you intend on doing "new row", by wrapping the whole row in some kind of element, or just by using a <br>
, so we'll assume br. 我不确定您打算如何通过将整个行包装在某种元素中或仅使用
<br>
做“新行”,所以我们假设br。 Give this a try: 试试看:
<?php
while ($auction = $result->fetch_assoc()):?>
<div class = "">
<h4><?=$auction['item_name']?></h4>
<img src='<?=$auction['item_image']?>' class='img-
responsive'>
<span id="countdown" class="timer">how</span>
<button class ="c-button" name ='bid'>Bid Now!</button>
</div>
<?php
if ($A % 3 == 2) echo "<br>"; // new row after every third item
$A++;
endwhile;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.