【问题标题】:get first object from result array php从结果数组php中获取第一个对象
【发布时间】:2015-11-11 05:53:30
【问题描述】:

我的应用上有视图文件,有代码数组我想在不进入循环的情况下获取该数组的第一个对象。

    <?php
$result = array_chunk($products->result_array(), 3);

foreach($result as $products){  ?>
    <table style="width:100% style="page-break-after:always;"   >

    <tr>

    <?php 
    foreach($products as $productArray){
        $product = (object) $productArray;
        echo '<td>';
    ?>

    <div style="width: 100%; height: 210px;  border: 1px solid #dddddd; margin: auto 5px 5px 0; padding: 5px;">
            <div class="box-header">
                <p class="box-title"><FONT SIZE=12><?php echo $product->product_name; ?></FONT></p>
            </div>
            <div style="height: 100px; text-align: center;">
                <?php echo '<img src="'.'uploads/'. $product->photo.'" class="img-responsive" style="height:100px !important; width: 150px !important"  />'; ?>
            </div>

            <div style="clear: both"></div>
            <table class="table table-responsive">
                <tr>
                    <th><FONT SIZE=12>ID</FONT></th>
                    <td><FONT SIZE=14><?php echo $product->product_id; ?></FONT></td>
                </tr>

$result 是我从表单中获取的对象数组。在下面你可以清楚地看到我正在将它分成 3 个数组并循环各个对象并将它们的详细信息获取到 html 中。

 <tr>
                    <th><FONT SIZE=12>ID</FONT></th>
                    <td><FONT SIZE=14><?php echo $product->product_id; ?></FONT></td>
                </tr>

我想获取第一个对象的详细信息,比如说想要获取结果数组的第一个对象的 $product->product_name 而不进入循环如何实现。

这里是完整的视图文件代码。

<!DOCTYPE html>
<html class="bg-black">
<head>
    <meta charset="UTF-8">
    <title><?php if(isset($title)) echo $title.' | '; ?>  Sales agent management software (SAMS) </title>

    <style>
        body{
            font-size: 9px;
            margin: 20px;
        }
        th,td,p,div,table,h3{margin:0;padding:0}

        @page { margin: 20px; }

        .header{
            border-bottom: 0px solid #dddddd;
            text-align: center;
            position: fixed; top: 0;
        }
        .footer { position: fixed; bottom: 0px; text-align: center }
        .pagenum:before { content: counter(page); }

    </style>
</head>

<body>




   <?php
    $usd = get_option('lkr_per_usd', 134);
    ?>



<div class="footer">

Page: <span class="pagenum"></span>, creation time : <?php echo date('l jS \of F Y h:i:s A') ?>, create by: <?php echo user_full_name(singleDbTableRow(loggedInUserData()['user_id'])); ?>, $ Rate : Rs. <?php echo $usd; ?> </div>


<br />
<div class="box-body">

    <?php
    $usd = get_option('lkr_per_usd', 134);
    ?>


    <?php
    $result = array_chunk($products->result_array(), 3);

    foreach($result as $products){  ?>
        <table style="width:100% style="page-break-after:always;"   >

        <tr>

        <?php 
        foreach($products as $productArray){
            $product = (object) $productArray;
            echo '<td>';
        ?>

        <div style="width: 100%; height: 210px;  border: 1px solid #dddddd; margin: auto 5px 5px 0; padding: 5px;">
                <div class="box-header">
                    <p class="box-title"><FONT SIZE=12><?php echo $product->product_name; ?></FONT></p>
                </div>
                <div style="height: 100px; text-align: center;">
                    <?php echo '<img src="'.'uploads/'. $product->photo.'" class="img-responsive" style="height:100px !important; width: 150px !important"  />'; ?>
                </div>

                <div style="clear: both"></div>
                <table class="table table-responsive">
                    <tr>
                        <th><FONT SIZE=12>ID</FONT></th>
                        <td><FONT SIZE=14><?php echo $product->product_id; ?></FONT></td>
                    </tr>
                    <tr>
                        <th><FONT SIZE=12>LKR</FONT></th>
                        <td><FONT SIZE=14><?php $lkr = get_selling_price($product);
                            echo  number_format(round($lkr, get_option('round_precision')) ); ?></FONT>
                        </td>
                    </tr>
                    <tr>
                        <th> <FONT SIZE=12>US $</FONT></th>
                        <td><FONT SIZE=14><?php echo number_format(round(lkr_to_usd($lkr), get_option('round_precision')) ); ?></FONT></td>
                    </tr>
                </table>
                        <?php $GLOBALS['a']= $product->product_id; ?>
        </div>

            </td>
        <?php } ?>

    </tr>

    <?php } ?>

    </table>
</div><!-- /.box-body -->


</body>

</body>
</html>

【问题讨论】:

  • 在视图中编码是完全错误的
  • @Abdulla 没关系..我添加了一半的代码..
  • 是的,阿卜杜拉是对的。你应该让你的视线远离 php 代码。比如$result = array_chunk($products-&gt;result_array(), 3);,你应该只使用ifforecho
  • @imsiso 请立即查看完整的视图文件。
  • 另外我认为你需要检查你的开始和结束标签,比如&lt;tr&gt;&lt;/tr&gt;&lt;td&gt;&lt;/td&gt;

标签: php arrays codeigniter multidimensional-array


【解决方案1】:

好吧,我说过你需要改变你编写代码的方式,但是关于你的具体问题,请使用这个:

$result = array('a', 'b', 'c', 'd', 'e');
reset($array);
$first = current($array);

你可以检查一下:

http://php.net/manual/en/function.reset.php

http://php.net/manual/en/function.current.php


但仍然是关于您的编码方式。你应该很快去 MVC 或这样的编程方式,所以你应该分开你的视图和编码逻辑

你可能有一个像view_profile.php 这样的页面,它将显示用户的信息。在常规编码中你有这个:

view_profile.php:

<?php session_start();

// you check sessions to see if the user is logged in and has the right to view this page.
// like:

if ($_SESSIONS['is_user_logged_in']){
   $username=$_SESSIONS['username'];
   $name=$_SESSIONS['name'];
   // ....
}else{
    header('location: ./login.php');// if user is not authenticated u redirect to login page
    exit();// prevents the rest of codes to be shown and executed 
}
?>
<html>
<head>

<title>View <?php echo $name; ?>'s profile</title>

</head>
<body>
   <div>Name: <span><?echo $name; ?></span></div>
   ......
</body>

</html>

但你可以这样做更好:

你有类似的文件

'view_profile.htm' // 保存 HTML 'view_profile.php // 保存 PHP 逻辑 'inc.php' // 包含一些有用的函数,可以帮助你编写 PHP 逻辑

view_profile.htm

<html>
<head>

<title>View <?php echo $name; ?>'s profile</title>

</head>
<body>
   <div>Name: <span><?echo $name; ?></span></div>
   ......
</body>

</html>

inc.php

<?php

function ses_start(){
    if(!session_id()){
        session_start();
   }
}

function ses_get($key){
    ses_start();
    if(!empty($_SESSION[$key])){
        return $_SESSION[$key];
    }
    return NULL;
}

function ses_set($key,$val){
    $_SESSION[$key]=$val;
}

function is_user_loggedin(){
    ses_start();
    if(!empty(ses_get('is_user_loggedin')){
        return true;
    }
    return false;
}

function go($to){
    header('location: '.$to);
    exit();
}

//and lots of useful functions that help u connect and work with data base.

view_profile.php

<?php 

include('inc.php');

if(is_user_loggedin(){
    $username=ses_get('username');
    $name=ses_get('name');
    //...
}else{
    go('login.php);
}

include('view_profile.html); // and u call the .htm file that holds the HTML the view file)

?>

这是一个将代码逻辑(php 代码)与视图(html 标签)分离的简单示例

你也可以搜索 MVC Model-View-Controller 并尝试使用简单的 MVC 框架。

【讨论】:

  • 欢迎您,但作为朋友,我建议您检查其余答案以及链接。因为你会看到你需要分离你的逻辑和视图。 - 祝你好运
猜你喜欢
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 2018-06-28
  • 2014-06-06
  • 2020-02-07
  • 2015-06-19
  • 2021-03-23
  • 2013-07-06
相关资源
最近更新 更多