drt5813 2017-02-14 10:41
浏览 18
已采纳

PHP比较2个数组和两个显示项目

I'm working on a uni assignment and ive been looking at some code for hours now and I'm lost :(

I need to take a session array storing cart items and then display the full item from another array

Data Array

$pumps = array(
    'GB96CSUN' => array(
        'img' => 'images/GB96CSUN.gif',
        'title' => '1938 Gilbert & Barker Model 96C Sunray',
        'desc' => 'Beautiful authentic yellow and orange paint scheme highlight this Sunray Gilbert & Barker early electric gas pump. Completely restored inside and out. Correct Gilbarco nozzle. Museum quality. (Rolling Stand Not Included)',
        'price' => '$5495 ',
        ),

    'TK39TLSIG' => array(
        'img' => 'images/TK39TLSIG.gif',
        'title' => '1939 Tokheim Model 39 (Tall) Signal Gasoline',
        'desc' => 'Impressive size and paint scheme, this Tokheim 39 Tall Signal Gasoline pump signaled that the end of the pre-war, "tall" pump era was coming to a close. This magnificent example with its vintage gas brand is near mint. Completely restored. Correct Tokheim nozzle. Near mint. (Rolling Stand Not Included)',
        'price' => '$6495',
        ),

Session Array

$_SESSION['cart'][0];

Array ( [0] => GP8002 [1] => GP792 [2] => RGP300A ) 

Display Code

foreach ($_SESSION['cart'] as $cartlist){
    if (in_array($cartlist, $pumps)){
        echo "<div class='container'>";
            echo    "<nav>";
                echo"<a href='#'><img src='images/$pid.jpg' height='250px'></a>";
            echo"</nav>";
            echo "<article>";
                echo  "<h1>{$cartlist['title']}</h1>";
                echo "<p>{$cartlist['desc']}</p>";
                echo "<p> Price :{$cartlist['price']</p>";
            echo "</article>";
        echo "</div>";
        } 
}

Cart Output

Array
(
    [0] => GP8002
    [1] => GP792
    [2] => RGP300A
)
  • 写回答

1条回答 默认 最新

  • doudi7570 2017-02-14 10:57
    关注

    What i assumed and then what is correct both are given below:-

    I have assumed that echo "<pre/>";print_r($_SESSION['cart']) is something like below:-

    Array ( 
      0 => 'GB96CSUN', 
      1 => 'GP792', 
      2 => 'TK39TLSIG' 
    );
    

    Based on this code need to be:-

    <?php
    error_reporting(E_ALL); // check all type of errors
    ini_set('display_errors',1); // display those errors
    session_start(); // start session to use session data
    $pumps = array(
    'GB96CSUN' => array(
        'img' => 'images/GB96CSUN.gif',
        'title' => '1938 Gilbert & Barker Model 96C Sunray',
        'desc' => 'Beautiful authentic yellow and orange paint scheme highlight this Sunray Gilbert & Barker early electric gas pump. Completely restored inside and out. Correct Gilbarco nozzle. Museum quality. (Rolling Stand Not Included)',
        'price' => '$5495 '
        ),
    'TK39TLSIG' => array(
        'img' => 'images/TK39TLSIG.gif',
        'title' => '1939 Tokheim Model 39 (Tall) Signal Gasoline',
        'desc' => 'Impressive size and paint scheme, this Tokheim 39 Tall Signal Gasoline pump signaled that the end of the pre-war, "tall" pump era was coming to a close. This magnificent example with its vintage gas brand is near mint. Completely restored. Correct Tokheim nozzle. Near mint. (Rolling Stand Not Included)',
        'price' => '$6495'
        )
    );
    
    // comment this below line while using the code   
    $_SESSION['cart'] = Array ( 0 => 'GB96CSUN', 1 => 'GP792', 2 => 'TK39TLSIG' ); // comment this line while using code
    
    foreach ($_SESSION['cart'] as $cartlist){
        if (in_array($cartlist, array_keys($pumps))){ // check here i used array_keys()?>
           <div class='container'>
               <nav>
                  <a href='#'><img src="<?php echo $pumps[$cartlist]['img'];?>" height='250px'></a>
               </nav>
               <article>
                  <h1><?php echo $pumps[$cartlist]['title'];?></h1>
                   <p><?php echo $pumps[$cartlist]['desc'];?></p>
                   <p> Price :<?php echo $pumps[$cartlist]['price'];?></p>
               </article>
           </div>
    <?php } }?>
    

    Output of my local screen:- http://i.share.pho.to/bbd07738_o.png (i have given path of my local file images)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?