阵列分为三类

时间:2011-07-22 06:26:48

标签: php arrays

我有一个产品清单,其中有9种产品如下所示,以便我制作一系列$产品并在其中添加所有九种产品。 其实我想把这个产品分为三类手机,笔记本电脑和相机,并在每个类别中放置三个产品,那么请问如何才能帮助我?

这是我的products.php页面

   <?php
/*
 Each product needs to be added in the array.
*/

$phone = array(

// Product key
"SKU001" =>
new product(
// Name
"iPhone&#8482;",
// Product page. Can be a custom page or the generic product.php page.
"product.php", 
// Product image
"images/iphone_1.jpg",
// Price 
100.99,
// Weight
0.25,
// Extended description
"iPhone is a revolutionary new mobile phone."),

"SKU002" =>
new product(
"iPod Nano",
"product.php", 
"images/ipod_classic_.jpg", 
112.95,
0.75,
"iPod classic now features an anodized aluminum and polished stainless steel ."
),

"SKU003" =>
new product(
"iPod Shuffle",
"product.php", 
"images/ipod_shuffle_1.jpg", 
100.95,
2,
"iPod shuffle can deliver a new musical experience every time you sync.."
),

"SKU004" =>
new product(
"iPod Touch",
"product.php", 
"images/ipod_touch_1.jpg", 
122.99,
1,
"iPod touch features the same multi-touch screen technology as iPhone.."
),

"SKU005" =>
new product(
"iPod Nano",
"product.php", 
"images/ipod_nano_1.jpg", 
100.95,
2,
"ts the small iPod with one very big idea: video."
),
"SKU006" =>
new product(
"Apple iPhone",
"product.php", 
"images/apple-iphone.gif", 
120.95,
2,
"power save mode .or reduse ur cell's lcd brightness ."
),
"SKU007" =>
new product(
"iPhone 3GS",
"product.php", 
"images/step1-promo-iphone3gs.png", 
400.95,
2,
"Phone 3GS works only with AT&T wireless service ."
),
"SKU008" =>
new product(
"iPhone 4 Unlocked",
"product.php", 
"images/step1-promo-iphone4.png", 
550.95,
2,
"FaceTime video calling, Retina display, HD video recording ."
),
"SKU009" =>
new product(
"iPhone business",
"product.php", 
"images/promo_business.png", 
750.95,
2,
"iPhone 4 is the most powerful, flexible, and intuitive phone ever.  ."
),
"SKU010" =>
new product(
"Nokia C5 5MP",
"product.php", 
"images/nokia-c5-00-5mp1.jpg", 
750.95,
2,
"3G Network.  ."
),
"SKU011" =>
new product(
"N9",
"product.php", 
"images/Nokia-N9-00-cyan.jpg", 
750.95,
2,
"Nokia-N9 is the most powerful, flexible, and intuitive phone ever.  ."
),
"SKU012" =>
new product(
"nokia-n950",
"product.php", 
"images/nokia-n950.jpg", 
750.95,
2,
"nokia-n950 is the most powerful, flexible, and intuitive phone ever.  ."
),
);

?>
<?php
/*
 Each product needs to be added in the array.
*/
$laptop = array(

"SKU013" =>
new product(
"MacBook",
"product.php", 
"images/macbook_1.jpg", 
500.95,
2,
"he new MacBook is the fastest ever."
),

"SKU014" =>
new product(
"MacBook Air",
"product.php", 
"images/macbook_air.jpg", 
600.95,
2,
"MacBook Air is ultrathin, ultraportable, and ultra unlike anything else.."
),
"SKU015" =>
new product(
"MacBook Air",
"product.php", 
"images/macbook_air.jpg", 
600.95,
2,
"MacBook Air is ultrathin, ultraportable, and ultra unlike anything else.."
),
"SKU016" =>
new product(
"MacBook Pro",
"product.php", 
"images/macbook_pro_1.jpg", 
2000.95,
2,
"The NVIDIA GeForce 8600M GT delivers exceptional graphics processing power. .."
),
"SKU017" =>
new product(
"Sony VAIO",
"product.php", 
"images/sony_vaio_1.jpg", 
1000.95,
2,
"Unprecedented power. The next generation of processing technology has arrived.."
),
"SKU018" =>
new product(
"HP LP3065",
"product.php", 
"images/hp_1.jpg", 
400.95,
2,
"Stop your co-workers in their tracks with the stunning new 30-inch diagonal HP LP3065 Flat Panel Monitor. .."
),
"SKU019" =>
new product(
"Apple-MC506L",
"product.php", 
"images/t_Apple-MC506L.jpg", 
400.95,
2,
"Apple-MC506L. .."
),
"SKU020" =>
new product(
"Asus-x42jy",
"product.php", 
"images/t_Asus-x42jy-vx161r.jpg", 
400.95,
2,
"Asus-x42jy. .."
),
"SKU021" =>
new product(
"Dell-1564",
"product.php", 
"images/t_Dell-1564.jpg", 
400.95,
2,
"Dell. .."
),
);

?>
<?php
/*
 Each product needs to be added in the array.
*/
$camera = array(
"SKU022" =>
new product(
"Canon EOS 5D",
"product.php", 
"images/canon_eos_5d.jpg", 
1000.95,
2,
"Canon's press material for the EOS 5D states that it 'defines (a) new D-SLR category&..."
),
"SKU023" =>
new product(
"Nikon D300",
"product.php", 
"images/nikon_d300.jpg", 
1200.95,
2,
"Engineered with pro-level features and performance, the 12.3-effective-megapixel D300 combine...."
),
"SKU024" =>
new product(
"Nikon L23",
"product.php", 
"images/small_nikon_l23_3.jpg", 
800.95,
2,
"10 Megapixels
5x Optical Zoom
2.7 LCD Screen"
),
"SKU025" =>
new product(
"Nikon L24",
"product.php", 
"images/small_nikon_l24_1.jpg", 
850.95,
2,
"14 Megapixels
3.6x Optical Zoom
2.7 LCD Screen"
),
"SKU026" =>
new product(
"Nikon 500",
"product.php", 
"images/small_nikon_p500_2.jpg", 
1250.95,
2,
"12 Megapixels
36x Optical Zoom
3.0 LCD Screen"
),

"SKU027" =>
new product(
"Nikon D90",
"product.php", 
"images/small_nikon_d90_4.jpg", 
1250.95,
2,
"12.3 Megapixels
Professional DSLR
NIKKOR AF-S 18-105mm VR Lens"
),
"SKU028" =>
new product(
"Canon A800",
"product.php", 
"images/small_canon_a800_1.jpg", 
1250.95,
2,
"10 Megapixels
3.3x Optical Zoom
2.5 LCD Screen"
),
"SKU029" =>
new product(
"Canon A1200",
"product.php", 
"images/small_canon_a1200_3.jpg", 
1250.95,
2,
"12 Megapixels
4x Optical Zoom
2.7 LCD Screen"
),
"SKU030" =>
new product(
"Canon A2200",
"product.php", 
"images/small_canon_a2200_c.jpg", 
1250.95,
2,
"14 Megapixels
4x Optical Zoom
2.7 LCD Screen"
),
"SKU031" =>
new product(
"Sony Cybershot S3000",
"product.php", 
"images/small_sony_s3000_3.jpg", 
1250.95,
2,
"10 Megapixels
4x Optical Zoom
2.7 LCD Screen"
),
"SKU032" =>
new product(
"Sony Cybershot W510",
"product.php", 
"images/small_sony_w510_1.jpg", 
1250.95,
2,
"12 Megapixels
4x Optical Zoom
2.7 LCD Screen"
),
"SKU033" =>
new product(
"Sony Cybershot W520",
"product.php", 
"images/small_sony_w520_2.jpg", 
1250.95,
2,
"14 Megapixels
5x Optical Zoom
2.7 LCD Screenn"
),


);

?>

这是我的Product.class.php页面

<?php


class Product {
    var $name;
    var $url;
    var $img;
    var $price;
    var $weight;
    var $description;

    function Product($name, $url, $img, $price, $weight, $description) {
        $this->name = $name;
        $this->url = $url;
        $this->img = $img;
        $this->price = $price;
        $this->weight = $weight;
        $this->description = $description;
    }

    function getName() {
        return $this->name;
    }
}
?>

我的phone.php页面显示了数据

 <?php


include_once "config/settings.php";
include_once "class/Product.class.php";
include_once "class/DownloadableProduct.class.php";
include_once "class/SecureAsset.class.php";
include_once "class/Cart.class.php";
include_once "config/products.php";

session_start();

//$_SESSION['SelectedProduct'] = $phone;
//echo 'Welcome to page #1';
$_SESSION['SelectedProduct'] = $phone;

// If no session exists, create one
if (!session_is_registered('cart')) {
    $_SESSION['cart'] = new cart;
} 

$cart = $_SESSION['cart'];

$title = "Catalog";
include "header.php"; 
?>

<div id="navmenu">
    <ul>
        <li class="first">
            <a href="cart.php">View Cart (<?
        if ($cart->getTotalItems() >= 2) {
            echo $cart->getTotalItems() . " items";
        } else {
            echo $cart->getTotalItems() . " item";
        }
?>)</a>
        </li>
<?
    if (!$cart->isEmpty()) {
?>
        <li class="last">
            <a href="invoice.php">Check Out</a>
        </li>
<?
    }
?>
    </ul>
</div>

<hr />


<div id="catalog">
    <h2>Phone</h2>


<?php
foreach ($phone as $id => $product) { 
?>
    <div class="product">
        <a href="<?= $settings->siteUrl . $product->url?>?id=<?=$id?>">
            <img src="<?= $product->img?>" alt="<?=$product->getName()?>" />
        </a>
        <h3><a href="<?= $settings->siteUrl . $product->url?>?id=<?=$id?>"><?=$product->getName()?></a></h3>
        <p>
        <?=$product->description?>
            <span class="product-price">$ <?=$product->price?></span>
        </p>
        <form action="cart.php" method="post">
            <input type="hidden" name="action" value="add" />
            <input type="hidden" name="qty" value="1" />
            <input type="hidden" name="id" value="<?=$id?>" />
            <button type="submit" class="button">Add</button>
        </form>
        <hr />
    </div>
<? } ?>
    <hr />
</div>

<? 
include "footer.php";
?>

laptop.php页面就在这里

<?php

include_once "config/settings.php";
include_once "class/Product.class.php";
include_once "class/DownloadableProduct.class.php";
include_once "class/SecureAsset.class.php";
include_once "class/Cart.class.php";
include_once "config/products.php";

session_start();


$_SESSION['SelectedProduct'] == $laptop;
$cat=$_GET['cat'];
//$id=$_GET['id'];
//$_SESSION['SelectedProduct'] = "laptop";
// If no session exists, create one
if (!session_is_registered('cart')) {
    $_SESSION['cart'] = new cart;
} 

$cart = $_SESSION['cart'];

$title = "Catalog";
include "header.php"; 
?>

<div id="navmenu">
    <ul>
        <li class="first">
            <a href="cart.php">View Cart (<?
        if ($cart->getTotalItems() >= 2) {
            echo $cart->getTotalItems() . " items";
        } else {
            echo $cart->getTotalItems() . " item";
        }
?>)</a>
        </li>
<?
    if (!$cart->isEmpty()) {
?>
        <li class="last">
            <a href="invoice.php">Check Out</a>
        </li>
<?
    }
?>
    </ul>
</div>

<hr />


<div id="catalog">
    <h2>Laptop</h2>


<?php
foreach ($laptop as $id => $product) { 
?>
    <div class="product">
        <a href="<?= $settings->siteUrl . $product->url?>?id=<?=$id?>">
            <img src="<?= $product->img?>" alt="<?=$product->getName()?>" />
        </a>
        <h3><a href="<?= $settings->siteUrl . $product->url?>?id=<?=$id?>"><?=$product->getName()?></a></h3>
        <p>
        <?=$product->description?>
            <span class="product-price">$ <?=$product->price?></span>
        </p>
        <form action="cart.php" method="post">
            <input type="hidden" name="action" value="add" />
            <input type="hidden" name="qty" value="1" />
            <input type="hidden" name="id" value="<?=$id?>" />
            <button type="submit" class="button">Add</button>
        </form>
        <hr />
    </div>
<? } ?>
    <hr />
</div>

<? 
include "footer.php";
?>

camera.php

<?php


include_once "config/settings.php";
include_once "class/Product.class.php";
include_once "class/DownloadableProduct.class.php";
include_once "class/SecureAsset.class.php";
include_once "class/Cart.class.php";
include_once "config/products.php";

session_start();
//$_SESSION['SelectedProduct'] = "camera";
$_SESSION['SelectedProduct'] = $camera;
// If no session exists, create one
if (!session_is_registered('cart')) {
    $_SESSION['cart'] = new cart;
} 

$cart = $_SESSION['cart'];

$title = "Catalog";
include "header.php"; 
?>

<div id="navmenu">
    <ul>
        <li class="first">
            <a href="cart.php">View Cart (<?
        if ($cart->getTotalItems() >= 2) {
            echo $cart->getTotalItems() . " items";
        } else {
            echo $cart->getTotalItems() . " item";
        }
?>)</a>
        </li>
<?
    if (!$cart->isEmpty()) {
?>
        <li class="last">
            <a href="invoice.php">Check Out</a>
        </li>
<?
    }
?>
    </ul>
</div>

<hr />


<div id="catalog">
    <h2>Camera</h2>


<?php
foreach ($camera as $id => $product) { 
?>
    <div class="product">
        <a href="<?= $settings->siteUrl . $product->url?>?id=<?=$id?>">
            <img src="<?= $product->img?>" alt="<?=$product->getName()?>" />
        </a>
        <h3><a href="<?= $settings->siteUrl . $product->url?>?id=<?=$id?>"><?=$product->getName()?></a></h3>
        <p>
        <?=$product->description?>
            <span class="product-price">$ <?=$product->price?></span>
        </p>
        <form action="cart.php" method="post">
            <input type="hidden" name="action" value="add" />
            <input type="hidden" name="qty" value="1" />
            <input type="hidden" name="id" value="<?=$id?>" />
            <button type="submit" class="button">Add</button>
        </form>
        <hr />
    </div>
<? } ?>
    <hr />
</div>

<? 
include "footer.php";
?>

和finaly product.php页面

<?php


include_once "config/settings.php";
include_once "class/Product.class.php";
include_once "class/DownloadableProduct.class.php";
include_once "class/SecureAsset.class.php";
include_once "class/Cart.class.php";
include_once "config/products.php";

session_start();
//echo 'Welcome to page #5';
//$_SESSION['SelectedProduct'] = "phone";

// If no session exists, create one

if (!session_is_registered('cart')) {

    $_SESSION['cart'] = new cart;

} 



$cart = $_SESSION['cart'];


$id=$_GET['id'];

$title = $products[$id]->name;
include "header.php"; 
?>
<div id="navmenu">
    <ul>
        <li class="first">
            <a href="cart.php">View Cart (<?
        if ($cart->getTotalItems() >= 2) {
            echo $cart->getTotalItems() . " items";
        } else {
            echo $cart->getTotalItems() . " item";
        }
?>)</a>
        </li>
<?

    if (!$cart->isEmpty()) {

?>
        <li class="last">
            <a href="invoice.php">Check Out</a>
        </li>

<?

    }

?>
    </ul>
</div>
<div id="catalog">
    <h2><?=$camera[$id]->name?></h2>

这里我正在使用session但无法在product.php页面本身处理

<?php
// $product = $phone[$id];

if ($_SESSION['SelectedProduct']='phone')
{
 $product = $phone[$id];
// echo $_SESSION["username"];
}
else if($_SESSION['SelectedProduct']='laptop')
{
    $product = $laptop[$id];
}
else
{
    $product = $camera[$id];
}


?>


    <div class="product">
        <h3><?=$product->name?></h3>
        <img src="<?=$product->img?>" alt="<?=$product->name?>" />
        <p>
        <?=$product->description?>
            <span class="product-price">$ <?=$product->price?></span>
        </p>
        <form action="cart.php" method="post">
            <input type="hidden" name="action" value="add" />
            <input type="hidden" name="qty" value="1" />
            <input type="hidden" name="id" value="<?=$id?>" />
            <button type="submit" class="button">Add</button>
        </form>
    </div>
    <hr />

</div>
<? 
include "footer.php";
?>

1 个答案:

答案 0 :(得分:1)

$products = array('phone' => array_slice($products, 0, 3), 'laptop' => array_slice($products, 3, 3), 'camera' => array_slice($products, 6, 3));

修改

<?php
/*
 Each product needs to be added in the array.
*/

class Product {
    var $name;
    var $url;
    var $img;
    var $price;
    var $weight;
    var $description;

    function Product($name, $url, $img, $price, $weight, $description) {
        $this->name = $name;
        $this->url = $url;
        $this->img = $img;
        $this->price = $price;
        $this->weight = $weight;
        $this->description = $description;
    }

    function getName() {
        return $this->name;
    }
}


$products = array(
// Product key
"SKU001" =>
new product(
// Name
"iPhone&#8482;",
// Product page. Can be a custom page or the generic product.php page.
"product.php",
// Product image
"images/iphone_1.jpg",
// Price
100.99,
// Weight
0.25,
// Extended description
"iPhone is a revolutionary new mobile phone."),
"SKU002" => new product("iPod Nano","product.php","images/ipod_classic_.jpg",112.95,0.75,"iPod classic now features an anodized aluminum and polished stainless steel ."),
"SKU003" => new product("iPod Shuffle","product.php","images/ipod_shuffle_1.jpg",100.95,2,"iPod shuffle can deliver a new musical experience every time you sync.."),
"SKU004" => new product("MacBook","product.php","images/macbook_1.jpg",500.95,2,"he new MacBook is the fastest ever."),
"SKU005" => new product("MacBook Air","product.php","images/macbook_air.jpg",600.95,2,"MacBook Air is ultrathin, ultraportable, and ultra unlike anything else.."),
"SKU006" => new product("MacBook fly","product.php","images/macbook_air.jpg",600.95,2,"MacBook Air is ultrathin, ultraportable, and ultra unlike anything else.."),
"SKU007" => new product("Nikon L23","product.php","images/small_nikon_l23_3.jpg",800.95,2,"10 Megapixels5x Optical Zoom2.7 LCD Screen"),
"SKU008" => new product("Nikon L24","product.php","images/small_nikon_l24_1.jpg",850.95,2,"14 Megapixels3.6x Optical Zoom2.7 LCD Screen"),
"SKU009" => new product("Nikon 500","product.php","images/small_nikon_p500_2.jpg",1250.95,2,"12 Megapixels36x Optical Zoom3.0 LCD Screen"),
);

$products = array('phone' => array_slice($products, 0, 3), 'laptop' => array_slice($products, 3, 3), 'camera' => array_slice($products, 6, 3));
print_r($products);

给出:

Array
(
    [phone] => Array
        (
            [SKU001] => Product Object
                (
                    [name] => iPhone&#8482;
                    [url] => product.php
                    [img] => images/iphone_1.jpg
                    [price] => 100.99
                    [weight] => 0.25
                    [description] => iPhone is a revolutionary new mobile phone.
                )

            [SKU002] => Product Object
                (
                    [name] => iPod Nano
                    [url] => product.php
                    [img] => images/ipod_classic_.jpg
                    [price] => 112.95
                    [weight] => 0.75
                    [description] => iPod classic now features an anodized aluminum and polished stainless steel .
                )

            [SKU003] => Product Object
                (
                    [name] => iPod Shuffle
                    [url] => product.php
                    [img] => images/ipod_shuffle_1.jpg
                    [price] => 100.95
                    [weight] => 2
                    [description] => iPod shuffle can deliver a new musical experience every time you sync..
                )

        )

    [laptop] => Array
        (
            [SKU004] => Product Object
                (
                    [name] => MacBook
                    [url] => product.php
                    [img] => images/macbook_1.jpg
                    [price] => 500.95
                    [weight] => 2
                    [description] => he new MacBook is the fastest ever.
                )

            [SKU005] => Product Object
                (
                    [name] => MacBook Air
                    [url] => product.php
                    [img] => images/macbook_air.jpg
                    [price] => 600.95
                    [weight] => 2
                    [description] => MacBook Air is ultrathin, ultraportable, and ultra unlike anything else..
                )

            [SKU006] => Product Object
                (
                    [name] => MacBook fly
                    [url] => product.php
                    [img] => images/macbook_air.jpg
                    [price] => 600.95
                    [weight] => 2
                    [description] => MacBook Air is ultrathin, ultraportable, and ultra unlike anything else..
                )

        )

    [camera] => Array
        (
            [SKU007] => Product Object
                (
                    [name] => Nikon L23
                    [url] => product.php
                    [img] => images/small_nikon_l23_3.jpg
                    [price] => 800.95
                    [weight] => 2
                    [description] => 10 Megapixels
5x Optical Zoom
2.7 LCD Screen
                )

            [SKU008] => Product Object
                (
                    [name] => Nikon L24
                    [url] => product.php
                    [img] => images/small_nikon_l24_1.jpg
                    [price] => 850.95
                    [weight] => 2
                    [description] => 14 Megapixels
3.6x Optical Zoom
2.7 LCD Screen
                )

            [SKU009] => Product Object
                (
                    [name] => Nikon 500
                    [url] => product.php
                    [img] => images/small_nikon_p500_2.jpg
                    [price] => 1250.95
                    [weight] => 2
                    [description] => 12 Megapixels
36x Optical Zoom
3.0 LCD Screen
                )

        )

)

我的解决方案一切都还可以