在 PHP 中循环从 MySQL 查询返回的数组

发布于 2024-11-29 02:39:12 字数 484 浏览 1 评论 0原文

我有一个应用程序的一部分,它以数组的形式循环返回 MySQL 查询(众所周知)。但是,我需要对某些返回的项目进行几种不同的格式设置,例如,一列需要日元,另一列需要美国货币,而返回的项目之一是图像的链接。

我将使用列的名称,但是我用来完成此操作的相同函数将用于许多不同的表。

这就是到目前为止我所拥有的循环。

while($row = mysql_fetch_array($result)) {      
    for($i=0;$i<=count($row);$i++) {
    if($row[i]==$row['Yen_Price']) {// I didn't expect this to work...but this is what I would like to do.
        echo "Hello";
    }
    echo "<td>" . $row[$i] . "</td>";
    }
}

I have a part of an application that loops through a return of a MySQL query (as we all know) in the form of an Array. However, I need several different format settings placed on some of the items returned, for example, one column needs Japanese currency, the other has American currency and one of the returned items is a link to an image.

I would use the names of the column, however this same function that I am using to accomplish this will be used for many different tables.

This is what I have for the loop so far.

while($row = mysql_fetch_array($result)) {      
    for($i=0;$i<=count($row);$i++) {
    if($row[i]==$row['Yen_Price']) {// I didn't expect this to work...but this is what I would like to do.
        echo "Hello";
    }
    echo "<td>" . $row[$i] . "</td>";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何必那么矫情 2024-12-06 02:39:12
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $key => $value) {
        if ($key == 'Yen_Price') {
            echo "Hello";
        }
        echo "<td>$value</td>";
    }
}

话虽如此,使用相同的函数来处理所有可能表中的所有结果很快就会变得相当难以管理。您应该自定义它以适应场合,如下所示:

while ($row = mysql_fetch_assoc($result)) {
    echo "<td>Foo: $row[foo]</td>";
    echo "<td>Bar: $row[bar]</td>";
}
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $key => $value) {
        if ($key == 'Yen_Price') {
            echo "Hello";
        }
        echo "<td>$value</td>";
    }
}

Having said that, using the same function to process all results from all possible tables will soon be rather unmanageable. You should customized this to fit the occasion like so:

while ($row = mysql_fetch_assoc($result)) {
    echo "<td>Foo: $row[foo]</td>";
    echo "<td>Bar: $row[bar]</td>";
}
愁以何悠 2024-12-06 02:39:12

我会针对每个表标记这些结果,但如果您希望它最终变得灵活,请尝试这个臭代码

// using mysql_fetch_assoc() as we don't need the numeric indices
while($row = mysql_fetch_assoc($result)) {
    foreach ($row as $col => $val) {
        echo '<td>';
        switch ($col) {
            case 'US_Price'  :
                printf('$%0.2f USD', $val);
                break;
            case 'Yen_Price' :
                printf('¥%0.2f', $val);
                break;
            case 'image'     :
                printf('<img src="%s">', htmlspecialchars($val));
                break;
        }
        echo '</td>';
    }
}

请注意,这是一个 已知的反模式,你真的应该考虑另一种方法来解决这个问题。

I'd markup these results specific to each table but if you want it to be ultimately flexible, try this smelly code

// using mysql_fetch_assoc() as we don't need the numeric indices
while($row = mysql_fetch_assoc($result)) {
    foreach ($row as $col => $val) {
        echo '<td>';
        switch ($col) {
            case 'US_Price'  :
                printf('$%0.2f USD', $val);
                break;
            case 'Yen_Price' :
                printf('¥%0.2f', $val);
                break;
            case 'image'     :
                printf('<img src="%s">', htmlspecialchars($val));
                break;
        }
        echo '</td>';
    }
}

Note that this is a known antipattern and you should really think about another way to approach the problem.

謌踐踏愛綪 2024-12-06 02:39:12

使用以下代码。您可以根据需要进行修改。

$select=" WRITE YOUR SELECT QUERY HERE ";
$queryResult= mysql_query($select);

//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();

//STORE ALL THE RECORD SETS IN THAT ARRAY 
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC)) 
{
    array_push($data_array,$row);
}


mysql_free_result($queryResult);


//TEST TO SEE THE RESULT OF THE ARRAY 
echo '<pre>';
print_r($data_array);
echo '</pre>';
// YOU CAN USE HERE FOR EACH LOOP AS PER YOUR REQUIREMENTS. 

谢谢

Use the below code. You can modify it as you want.

$select=" WRITE YOUR SELECT QUERY HERE ";
$queryResult= mysql_query($select);

//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();

//STORE ALL THE RECORD SETS IN THAT ARRAY 
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC)) 
{
    array_push($data_array,$row);
}


mysql_free_result($queryResult);


//TEST TO SEE THE RESULT OF THE ARRAY 
echo '<pre>';
print_r($data_array);
echo '</pre>';
// YOU CAN USE HERE FOR EACH LOOP AS PER YOUR REQUIREMENTS. 

Thanks

﹎☆浅夏丿初晴 2024-12-06 02:39:12

在我成为框架狂热者之前,我曾经采用过一些不同的方法。我的数据库库有一组方法可以返回记录集数组。这样我就可以将数据库交互与使用记录集的方式完全分开。完成此操作后,可以轻松设置网格模板,该模板可以查看数组,然后采取相应的操作。这是一些伪代码

$recordSets = $db->returnRecordSets("select some, columns from tablename");//extra param if I need array to be  associative
$recordSetsCount = count($recordSets);
if($recordSetsCount == 0){ echo 'Nothing to be done!'; //exit or return or break here}
for($i=0; $i< $recordSetsCount; $i++ == 0){
  $recordSet = $recordSets[$i];
  /*Inspect the $recordSet array and use it*/
}

Before I became a framework fanatic I used to have a bit different approach. My db library had set of methods that returned me array of record sets. This way I keep my db interaction totally separate from how I consume the record sets. Having done this, its easy to set up a grid template which can look at array and then act accordingly. Here is some pseudo code

$recordSets = $db->returnRecordSets("select some, columns from tablename");//extra param if I need array to be  associative
$recordSetsCount = count($recordSets);
if($recordSetsCount == 0){ echo 'Nothing to be done!'; //exit or return or break here}
for($i=0; $i< $recordSetsCount; $i++ == 0){
  $recordSet = $recordSets[$i];
  /*Inspect the $recordSet array and use it*/
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文