/    Sign up×
Community /Pin to ProfileBookmark

if ($showP11) print item[array]

hi

this is what is currently on page1.php

[code=php]Item Name: <?php print $productnameP11 ?><br>
Item Number: <?php print $productcodeP11 ?><br>
Quantity: <?php print $quantityP11 ?><br>
Size: <?php print $sizeP11 ?><br>
Type: <?php print $typeP11 ?><br>
Price: £<?php print $priceP11 ?>[/code]

but 10 of them so P11–>P110

what i am trying to do is make it

<?php
if ($showP11==1){
print “XX”;
}
?>

where XX is the above code, item number etc

so the script would have the above code only once but instead of P11 you would have like an array, of P11–>P110 and then instead of print “XX”; would be like print “item[P11]”

thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@crh3675Feb 15.2004 — Where is $p11 defined? Is it from user input on a form?
Copy linkTweet thisAlerts:
@96turnerriauthorFeb 15.2004 — no sorry $P11 is not a variable, the variables are defined from another php file what i was thinking is

<?php

$num = array ("31", "32", "33", "34", "35", "36", "37", "38", "39", "310");

$item[x] = "Item Name: $productnameP$num[x] <br>Item Number: $productcodeP$num[x] <br>Quantity: $quantityP$num[x] <br>Size: $sizeP$num[x] <br>Type: $typeP$num[x] <br>Price: £$priceP$num[x] ";

if ($showP31==1){

print "$item[31]";

}

?>

but this doesnt work, the if () will be continued to

if ($showP310==1){

print "$item[310]";

}

the idea is to save on space and stop coding a seperate bit for each item on this page there are ten but on some pages there are say 25

thanks Rich
Copy linkTweet thisAlerts:
@crh3675Feb 15.2004 — Try this instead:

<i>
</i>&lt;?

class Inventory{

<i> </i>var $Items;

<i> </i>function Inventory(){
<i> </i> $this-&gt;Items=Array();
<i> </i>}

<i> </i>function AddItem($name,$code,$qty,$size,$type,$price){
<i> </i> $x=count($this-&gt;Items)==0 ? 0 : count($this-&gt;Items);
<i> </i> $this-&gt;Items[$x]=new Item($name,$code,$qty,$size,$type,$price);
<i> </i>}

<i> </i>function GetItemByCode($code){
<i> </i> $result="";
<i> </i> for($i=0;$i&lt;count($this-&gt;Items);$i++){
<i> </i> if($this-&gt;Items[$i]-&gt;code==$code){
<i> </i> $result=$this-&gt;Items[$i];
<i> </i> break;
<i> </i> }
<i> </i> }
<i> </i> return $result;
<i> </i>}



}

class Item extends Inventory{

<i> </i>var $name;
<i> </i>var $code;
<i> </i>var $qty;
<i> </i>var $size;
<i> </i>var $type;
<i> </i>var $price;

<i> </i>function Item($name,$code,$qty=0,$size="",$type="",$price){
<i> </i> $this-&gt;name=$name;
<i> </i> $this-&gt;code=$code;
<i> </i> $this-&gt;qty=$qty;
<i> </i> $this-&gt;size=$size;
<i> </i> $this-&gt;type=$type;
<i> </i> $this-&gt;price=$price;
<i> </i>}
}

// ##############################
// Extract the code above here into a separate file
// and include it within your page.
// Initiate Inventory Below
// Add items by calling the AddItem method
// of the Inventory class
//
// syntax: AddItem(name, code, quantity, size, type, price);
//
// Once you have done this,
// #############################

$Inv=new Inventory();

$Inv-&gt;AddItem("car","001",1,"small","none",55);
$Inv-&gt;AddItem("dog","002",1,"small","none",55);

$item=$Inv-&gt;GetItemByCode("001");
print "Name :".$item-&gt;name;
print "&lt;br&gt;Code :".$item-&gt;code;
print "&lt;br&gt;Size :".$item-&gt;size;
print "&lt;br&gt;Type :".$item-&gt;type;
print "&lt;br&gt;Price : $".number_format($item-&gt;price,2);

?&gt;
×

Success!

Help @96turnerri spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...