简体   繁体   English

我想使用php以网格形式显示来自mySQL数据库的项目,但我正在获取

[英]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.

相关问题 我从php创建一个HTML表单,该表单可以选择表格中的项目-我想使用javascript发送带有一些信息的所选项目 - From php I create a HTML form that mult selects items in a table - I want to send the selected items with some bits of information using javascript 我想显示数据库中的数据。如果我们新添加了一个表单数据,我想在不使用 codeigniter 刷新的情况下在显示中附加新帖子 - I want to display data from database.if we newly add a form data ,i want to append the new post on display without refresh using codeigniter 我想用我从数据库中使用Ajax从另一个数据库文件中获取的数据填充表单元素 - I want to populate my form elements with data i fetch from my database in another php file using ajax 我正在尝试从mysql数据库在网页上放置图形我遇到一个错误$未定义如何解决? - I,m trying to put graph on webpage from mysql database I'm getting one error $ is not defined How can i fix it? 我想使用php将选定的记录从mysql打印到打印机 - I want to print selected records from mysql to printer using php 我正在编写代码以使用 ExpressJS 将用户的表单输入上传到 Mysql 数据库,但出现无法发布的错误 - I am writing a code to upload form inputs from user to Mysql Database using ExpressJS, but i am getting error as Cannot Post 如何使用JavaScript从MYSQL数据库显示文本 - How do I display text from a MYSQL database using javascript 我想使用 react 以 3*X 的网格方式显示图像 - I want to display images in a grid fashion of 3*X using react 我想在 React.js 和 Mysql 的表单列(自动填充数据)中显示从 api 获取的数据 - I want to display fetched data from api in form columns (auto fill data) in React.js and Mysql 我想使用switch语句显示所选商品的价格值 - I want to display the price value for selected items using switch statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM