0

I have a Json result like this :

Array (

[0] => Array (
     [Street] => Street_name 
     [status] => Best_Shop 
     [Shop] => Array ( [0] => Array ( 
                    [Name] => Bakery_Shop 
                    [Owner] => John
                    [Type] => 0 
                    [Food] => Cake 
                    [Drink] => Coffee 
                    [Best_Customer] => All
                    [a] => Good 
                    [b] => Normal
                    [c] => Bad  
               [1] => Array ( 
                    [Name] => Junk_Foodshop 
                    [Owner] => Mike
                    [Type] => 0 
                    [Food] => Burger 
                    [Drink] => Coke 
                    [Best_Customer] => All
                    [a] => Good 
                    [b] => Normal
                    [c] => Bad ) 
    [Rate] => Average 
    [Signature] => Boss ) )

And i am trying to create a shop table with rows displaying each stat like this :

Name          Owner Type Food   Drink    Best_Customer  a    b      c

Bakery_Shop   John   0   Cake   Coffee       All        Good Normal Bad

Junk_Foodshop Mike   0   Burger Coke         All        Good Normal Bad

I currently have this code written up and although i am getting the data its not quite working the way i want it to be.

****From json_shop.php****

function getAstCard(){
    $json = array();
    $json[0] = array(
        "Street" => "Street_name ",
        "status" => "Best_Shop ",
        "Shop" => $Shop,
        "Rate" => "Average ",
        "Signature" => $conf->get_IXAstUser()
    );
    return $json;
}
**Into jsonTable.php**

    <?php
    include 'json_shop.php';

    $AstCardTable.='<table style="width:990px;" id="shopcard">';
    $AstCardTable.='<thead>';
    $AstCardTable.='<tr>';
    $AstCardTable.='    
                            <th>Name</th>
                            <th>Owner</th>
                            <th>Type</th>
                            <th>Food</th>
                            <th>Drink</th>
                            <th>Best_Customer</th>
                            <th>a</th>
                            <th>b</th>
                            <th>c</th>
                            </tr>
                            </thead>
                            <tbody>';

    $AstCard = getAstCard();
    if (count($AstCard) > 0) {
        for ($i = 0; $i < count($AstCard); $i++) {
            $AstCardRecord = $AstCard[$i];
                $AstCardTable.='<tr>';
                $AstCardTable.='<td>' . $AstCardRecord['Name'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Owner'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Type'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Food'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Drink'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['Best_Customer'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['a'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['b'] . '</td>';
                $AstCardTable.='<td>' . $AstCardRecord['c'] . '</td>';
                $AstCardTable.='</tr>';
        }
    } else {
        $AstCardTable.='<tr>';
        $AstCardTable.='<td style="font-family: Verdana;font-weight: bold;font-size: 12px;" colspan=9>Temporarily no data in the list</td>';
        $AstCardTable.='</tr>';
    }

    $AstCardTable.='</tbody>';
    $AstCardTable.='</table>';

    echo $AstCardTable;


    ?>

Does anyone with experience in this topic and see if any problem about my code? Any help would be greatly appreciated.Thank you .

3
  • You need to return return $json[0]['Shop']; from getAstCard method. Commented Jul 22, 2014 at 5:55
  • yes .I already call that function but it still not showing the output table Commented Jul 22, 2014 at 5:57
  • @art, I added my updated code below, Please check if it can resolve your issue. Commented Jul 22, 2014 at 6:00

1 Answer 1

1

You need to return return $json[0]['Shop']; from getAstCard method.

Otherwise, change the variable in your loop a bit :

$AstCard = getAstCard();
$AstCardShop = $AstCard[0]['Shop'];
if (count($AstCardShop) > 0) {
    for ($i = 0; $i < count($AstCardShop); $i++) {
            $AstCardRecord = $AstCardShop[$i];

            $AstCardTable.='<tr>';
            $AstCardTable.='<td>' . $AstCardRecord['Name'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Owner'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Type'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Food'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Drink'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['Best_Customer'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['a'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['b'] . '</td>';
            $AstCardTable.='<td>' . $AstCardRecord['c'] . '</td>';
            $AstCardTable.='</tr>';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks .it working but why it only show the row for bakery_shop .but not showing the row for Junk_Foodshop ?
$AstCard[0]['Shop'] contains 2 elements. So, count of that variable should come as 2 and it should loop 2 times. Please check with the above code, I changed a little. You might checked with my previous code. If still you are facing the same issue, then do echo count($AstCardShop); exit; before the loop and check what is the count.

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.