1

I have view file on my app there is code array I want to get first object of that array without going in loop.

    <?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 is array of object I'm getting from a form. In below you can clearly see I'm chunking it to 3 more array and looping though individual objects and getting their details to html for example.

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

I want to get the first object details let's say want get $product->product_name of first object of result array without going in loop how to achieve that.

here is the view file result.

here is complete view file code.

<!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>
6
  • 1
    Coding in view is completely wrong Commented Nov 11, 2015 at 5:54
  • @Abdulla it's okey.. i added half of code.. Commented Nov 11, 2015 at 5:57
  • yes Abdulla is right. u should keap your view away of php codes. like $result = array_chunk($products->result_array(), 3); And u should use only if, for , echo Commented Nov 11, 2015 at 5:59
  • @imsiso please check now complete view file. Commented Nov 11, 2015 at 6:00
  • Also i think u need to check your opening and closing tags like <tr> and </tr> or <td> and </td> Commented Nov 11, 2015 at 6:01

1 Answer 1

2

ok man as i said u need to change your way of writing codes but about your specific question use this:

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

you can check this:

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

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


But still about your way of coding. u should soon go to MVC or such ways of programming so u should separate your view and coding logics

like u may have a page like view_profile.php which is going to show user's information. in regular coding u have this:

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>

But in a better way u can do it like this:

you have files like

'view_profile.htm' // holds the HTML 'view_profile.php // holds the PHP logic 'inc.php' // holds some useful functions that help you in writing PHP logic

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)

?>

this was a simple sample of separating codes logic(php codes) from views(html tags)

and also u may search about MVC Model-View-Controller and try working with simple MVC frameworks.

Sign up to request clarification or add additional context in comments.

1 Comment

your welcome but as a friend i advice you to check the rest of the answer and also the links. cause u will see that you need to separate your logic and view. - good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.