简体   繁体   English

PHP显示表中的交替记录

[英]PHP Display Alternating Records from Table

Ok, so, you guys were extremely helpful with my last issue, so , why not try again? 好吧,那么,你们对我上一期的问题非常有帮助,所以,为什么不再试一次呢?

I have a page where I'm trying to display records from a table in an alternating style (left vs right aligned). 我有一个页面,我试图以交替的方式显示表格中的记录(左对齐右对齐)。 While I know the importance of prepared statements and such, I just need to see that it works correctly before I clean up the code (call it my OCD). 虽然我知道预处理语句的重要性等等,但我需要在清理代码之前看到它正常工作(称之为我的OCD)。

I'm having issues getting any records to display. 我有问题要显示任何记录。 Essentially, I'm running a foreach loop inside a scrollable div in hopes that it takes each record and displays them appropriately. 本质上,我在一个可滚动的div中运行一个foreach循环,希望它能够获取每个记录并适当地显示它们。

Thoughts? 思考?

<div style="height:450px; overflow:auto;">
<?php 
//$gID = sprintf("%d", $_GET['gearID']);
$query = "SELECT * FROM GearTable";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

/*trick the items into aligning left or right in alternating pattern*/
$x++; 
$align = ($x%2 == 0)? 'left': 'right';

//if rows exist
if ($num_rows > 0)
{
    foreach ($result as $row)
    {
        echo "
                <div style='margin-top:8px;'>
                    <img src='{$row['GearImgLoc']}' width='200' align='left' class='imgGear'>
                    <h2>{$row['GearName']}</h2>
                    <p class='textP'><b>Gear Manufacturer:</b> {$row['GearMFG']}</p>
                    <p class='textP'><b>Gear Description:</b> {$row['GearDesc']}</p>
                    <p><span class='posted'>Date Posted: {$row['DatePosted']} | Posted By: {$row['PostedBy']}</span></p>
                    <p><a href='gear.php?type=edit&id={$row['GearID']}'>Edit</a> | <a href='gear.php?type=delete&id={$row['GearID']}'>Discard</a> |  <a href='preview.php?gearID={$row['GearID']}'>Preview</a></p>
                </div>
            ";  
    }
}
else
{
    echo "No Records Available At This Time";
}
?>
</div>

You need to use mysql_result() to use the result object. 您需要使用mysql_result()来使用结果对象。

For example: 例如:

$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = mysqli_query($link, $query);

/* numeric array */
$row = mysqli_fetch_array($result, MYSQLI_NUM);

Taken from the PHP documentation. 取自PHP文档。

For more information: http://www.php.net/manual/en/class.mysqli-result.php 欲了解更多信息,请访问: http//www.php.net/manual/en/class.mysqli-result.php

Just for the sake of the question: 只是为了这个问题:

This is how it works using MySQL (which is deprecated as of PHP 5.5.0 therefore I suggest you to move to MySQLi or PDO as soon as possible): 这是它如何使用MySQL(从PHP 5.5.0开始不推荐使用,因此我建议你尽快转移到MySQLi或PDO):

mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %s  Name: %s", $row[0], $row[1]);  
}

mysql_free_result($result);

Something like this should put you in the right direction: 这样的事情会让你朝着正确的方向前进:

foreach ($result as $i => $row) {

     $class_name = ( $i % 2 == 0 ? 'left' : 'right' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM