Urgent help needed please


#1

i am a new php user currently designing an order form but iam currently have trouble, i have a text file created but it php does not decrease the values properly, can anyone help?


#2

First of all you should read http://www.phphelp.com/forums/viewtopic.php?t=4089

You need to provide us with more than just a vague description of the problem…

  • What is it doing?
  • What is it NOT doing that maybe it should?
  • What error messages are displayed?
  • What is the section of code that you think is the problem?
  • What have you tried?
  • What are the results of what you tried?

Also helpful is

  • What O/S and version
  • What Webserver and version
  • What version of PHP and MySQL (if appropriate).

We will be glad to help you if you Help us help you


#3

thank you for getting back to me
, i am using windows xp home
using xitami webserver

what it is doing is scrubing all the values in the text file once i save a sale, what it should be doing is decreasing the values by 1

would it be possible if i can have ure email adress and speak on msn or something because it will be alot easier to explain

here is my code incase you dont want to do that

[php]

<?php //Warning incase something is out of stock!// $warning= " Please Return to Homepage to cpu Another Selection"; ////////////////Steven Russell////////////////////////////////// // pass variables to be written, open text files, reduce stock, validate////////////////////////// // Set the string to be written to the file//////////////////// $value = "fname: $fname rn"; $value .= "sname: $snamern"; $value .= "E-mail: $email rn"; $value .= "pro: $prorn"; $value .= "cpur: $cpurn"; $value .= "motherb: $motherbrn"; $value .= "Ramtotal: $totrn"; $value .= "Ramcost: $costrn"; $value .= "total cost: $totcostrn"; //Validate to make sure all items are entered. if not exit/// if(($fname=="")||($sname=="")||( $pro=="")||( $motherb=="")||( $tot=="")||( $cost=="")||( $email=="")||( $cpu=="")||( $totcost=="")){ exit; } //////////////// Open files for reading (stock)/////////////// $fp = fopen("stock.txt","rb")or die ("could not open stock file"); $dat = ""; while(!feof($fp)) { $dat.= fgets($fp,4096); } fclose($fp); $val=explode("rn",$dat); ///////////reduce stock levels////////// { if ($ram64 == "1") ($val[0] = $val[0]-1);}{ if ($ram128 == "1") ($val[1] = $val[1]-1);}{ if ($ram256 == "1") ($val[2] = $val[2]-1);}{ if ($ram512 == "1")($val[3] = $val[3]-1);}{ if ($ram1024 == "1")($val[4] = $val[4]-1);}{ if ($probath == "1") ($val[5] = $val[5]-1);}{ if ($prohent == "1") ($val[6] = $val[6]-1);}{ if ($motherbbathtot1 == "1") ($val[7] = $val[7]-1);}{ if ($motherbbathtot2== "1")($val[8] = $val[8]-1);}{ if ($motherbhenttot1== "1")($val[9] = $val[9]-1);}{ if ($motherbhenttot2 == "1")($val[10] = $val[10]-1);} //////////////out of stock validation////////// if ($val[0] < 1) { $os64 = " 64 MB RAMM is out of stock "; $val[0]="0"; $totcost = $warning ; } if ($val[1] < 1) { $os128 = " 128 MB RAMM is out of stock "; $val[1]="0"; } if ($val[2] < 1) { $os256 = " 256 MB RAMM is out of stock "; $val[2]="0"; $totcost = $warning; } if ($val[3] < 1) { $os512 = " 512 MB RAMM is out of stock "; $val[3]="0"; $totcost = $warning; } if ($val[4] < 1) { $os1024 = " 1024 MB RAMM is out of stock "; $val[4]="0"; $totcost = $warning; } if ($val[5] < 1) { $pro = " $cpu is out of stock "; $val[5]="0"; } if ($val[6] < 1) { $pro = " $cpu is out of stock"; $val[6]="0"; $totcost = $warning; } if ($val[7] < 1) { $motherb = " Motherboard is out of stock "; $val[7]="0"; $totcost = $warning; } if ($val[8] < 1) { $motherb = " Motherboard is out of stock "; $val[8]="0"; $totcost = $warning; } if ($val[9] < 1) { $motherb = " Motherboard is out of stock "; $val[9]="0"; $totcost = $warning; } if ($val[10] < 1) { $motherb = " Motherboard is out of stock"; $totcost = $warning ; $val[10]="0"; } /////////////////assign values for over writing/////////// $val = "$val[0] rn"; $val .= "$val[1]rn"; $val .= "$val[2]rn"; $val .= "$val[3]rn"; $val .= "$val[4]rn"; $val .= "$val[5]rn"; $val .= "$val[6]rn"; $val .= "$val[7]rn"; $val .= "$val[8]rn"; $val .= "$val[9]rn"; $val .= "$val[10]rn"; ////// Open the file for writing/////////////////////// $fp = fopen("stock.txt", "w") or die("Couldn't open stock.txt for writing!"); $numBytes = fwrite($fp, $val ) or die("Couldn't write values to file!"); fclose($fp); $fp = fopen("order.txt", "a") or die("Couldn't open order.txt for writing!"); $numBytes = fwrite($fp, $value) or die("Couldn't write values to file!"); fclose($fp); /////////END OF PHP///////////////// ?> Final Page

 

    </div>
    <div align="center"> </p> 
    
    </div>
  <td align="center" valign="top"><div align="center">
      <p>&nbsp;</p>
    </div>
    <div align="center">Thank you For your order <?php echo "   $fname " ?></div>
    </h3>
    <p align="center"><?php echo "  Baseunit  : ? 400       " ?></p>
    <p align="center"><?php echo "  $cpu processor  : ? $pro                " ?> 
    <p align="center"><?php echo "  $tot Ram  : ? $cost         " ?>
	<p align="center"><?php echo "  $cpu MotherBoard  : ? $motherb " ?> 
    <p align="center"><font color="#FF0000"><?php echo "  Total  Price including all Components : ? $totcost  " ?></font> 
    
    <p align="center">The above has been ordered. You will receive an email 
      in the next 24 hours to confirm this. 
   
    <p align="center">Email Will be sent to <?php echo "$email"?>
   <div align="center"> 
      
      </div>
[/php] [b]MOD EDIT: Added PHP bbCode Tags and removed excessive white space[/b]

#4

which part/section of the code is giving the problem. I don’t want to wade through all the code to find the problem in 1 line.

what it is doing is scrubing all the values in the text file once i save a sale, what it should be doing is decreasing the values by 1

I don’t understand what you are saying… do you mean that the text file is being completely rewritten rather then the sale being concatinated on to the end or values altered within the file? Check out the append mode (Used in the fopen http://www.php.net/manual/en/function.fopen.php ) for fwrite (http://www.php.net/fwrite).

As for your last question note again the thread peg110 sent you (the first thing under posting guidelines - Private Messages and Email).


#5

thank you for the reply, here is an example of the text file whats it is doing before a sale and after a sale

Before
20
20
20
20
20
20
20
20
20
20
20

After
20
0

0

What it should do is some of the values should decrease to 19 depending what item is chosen, i am really stuck so can somebody please please sort out the problem or give me a guidline on how to


#6

Ok, First of all you are killing me with your coding format (or lack thereof). Not that it’s critical that code be writen in a certain way, but it makes it much easier to read if you follow certain standards of practice.

White space hurts nothing in the code so use it as needed. However to much white space makes it almost as difficutlt (thus the mod edit of your original code post).

Also you should get familiar with using the posting features (the URL, PHP, CODE, etc… Buttons). It also helps to get your point across.

Now that being said. You seem to have a thing about the CURLY { } braces… On your multiple IF statements you are surrounding them in curly braces. Again, when I went back to put them in a standard coding format, I would venture to say that that is your problem (if not part of it anyway)

Just a note about IF statements. If there is only ONE line of code to execute from the IF then you don’t need the braces at all.

Below is your code put in a more standard format. It makes things eaiser to find (particularly errors). As a general rule when you have a sub-section, you should indent it until the end of that subsection).

[php]

<?php //Warning incase something is out of stock!// $warning= " Please Return to Homepage to cpu Another Selection"; ////////////////Steven Russell////////////////////////////////// // pass variables to be written, open text files, reduce stock, validate////////////////////////// // Set the string to be written to the file//////////////////// $value = "fname: $fname rn"; $value .= "sname: $snamern"; $value .= "E-mail: $email rn"; $value .= "pro: $prorn"; $value .= "cpur: $cpurn"; $value .= "motherb: $motherbrn"; $value .= "Ramtotal: $totrn"; $value .= "Ramcost: $costrn"; $value .= "total cost: $totcostrn"; //Validate to make sure all items are entered. if not exit/// if(($fname=="")||($sname=="")||( $pro=="")||( $motherb=="")||( $tot=="")||( $cost=="")||( $email=="")||( $cpu=="")||( $totcost=="")){ exit; } //////////////// Open files for reading (stock)/////////////// $fp = fopen("stock.txt","rb")or die ("could not open stock file"); $dat = ""; while(!feof($fp)) { $dat.= fgets($fp,4096); } fclose($fp); $val=explode("rn",$dat); ///////////reduce stock levels////////// { if ($ram64 == "1") ($val[0] = $val[0]-1); } { if ($ram128 == "1") ($val[1] = $val[1]-1); } { if ($ram256 == "1") ($val[2] = $val[2]-1); } { if ($ram512 == "1")($val[3] = $val[3]-1); } { if ($ram1024 == "1")($val[4] = $val[4]-1); } { if ($probath == "1") ($val[5] = $val[5]-1); } { if ($prohent == "1") ($val[6] = $val[6]-1); } { if ($motherbbathtot1 == "1") ($val[7] = $val[7]-1); } { if ($motherbbathtot2== "1")($val[8] = $val[8]-1); } { if ($motherbhenttot1== "1")($val[9] = $val[9]-1); } { if ($motherbhenttot2 == "1")($val[10] = $val[10]-1); } //////////////out of stock validation////////// if ($val[0] < 1) { $os64 = " 64 MB RAMM is out of stock "; $val[0]="0"; $totcost = $warning ; } if ($val[1] < 1) { $os128 = " 128 MB RAMM is out of stock "; $val[1]="0"; } if ($val[2] < 1) { $os256 = " 256 MB RAMM is out of stock "; $val[2]="0"; $totcost = $warning; } if ($val[3] < 1) { $os512 = " 512 MB RAMM is out of stock "; $val[3]="0"; $totcost = $warning; } if ($val[4] < 1) { $os1024 = " 1024 MB RAMM is out of stock "; $val[4]="0"; $totcost = $warning; } if ($val[5] < 1) { $pro = " $cpu is out of stock "; $val[5]="0"; } if ($val[6] < 1) { $pro = " $cpu is out of stock"; $val[6]="0"; $totcost = $warning; } if ($val[7] < 1) { $motherb = " Motherboard is out of stock "; $val[7]="0"; $totcost = $warning; } if ($val[8] < 1) { $motherb = " Motherboard is out of stock "; $val[8]="0"; $totcost = $warning; } if ($val[9] < 1) { $motherb = " Motherboard is out of stock "; $val[9]="0"; $totcost = $warning; } if ($val[10] < 1) { $motherb = " Motherboard is out of stock"; $totcost = $warning ; $val[10]="0"; } /////////////////assign values for over writing/////////// $val = "$val[0] rn"; $val .= "$val[1]rn"; $val .= "$val[2]rn"; $val .= "$val[3]rn"; $val .= "$val[4]rn"; $val .= "$val[5]rn"; $val .= "$val[6]rn"; $val .= "$val[7]rn"; $val .= "$val[8]rn"; $val .= "$val[9]rn"; $val .= "$val[10]rn"; ////// Open the file for writing/////////////////////// $fp = fopen("stock.txt", "w") or die("Couldn't open stock.txt for writing!"); $numBytes = fwrite($fp, $val ) or die("Couldn't write values to file!"); fclose($fp); $fp = fopen("order.txt", "a") or die("Couldn't open order.txt for writing!"); $numBytes = fwrite($fp, $value) or die("Couldn't write values to file!"); fclose($fp); /////////END OF PHP///////////////// ?> Final Page

 

    </div>
    <div align="center"> </p> 
    
    </div>
  <td align="center" valign="top"><div align="center">
      <p>&nbsp;</p>
    </div>
    <div align="center">Thank you For your order <?php echo "   $fname " ?></div>
    </h3>
    <p align="center"><?php echo "  Baseunit  : ? 400       " ?></p>
    <p align="center"><?php echo "  $cpu processor  : ? $pro                " ?> 
    <p align="center"><?php echo "  $tot Ram  : ? $cost         " ?>
	<p align="center"><?php echo "  $cpu MotherBoard  : ? $motherb " ?> 
    <p align="center"><font color="#FF0000"><?php echo "  Total  Price including all Components : ? $totcost  " ?></font> 
    
    <p align="center">The above has been ordered. You will receive an email 
      in the next 24 hours to confirm this. 
   
    <p align="center">Email Will be sent to <?php echo "$email"?>
   <div align="center"> 
      
      </div>
[/php]

So Finally I would suggest removing ALL the Curly Braces around the IF section for reducing your stock.

Good luck


#7

thankyou for taking time in getting back to me
however as things stand i am still having the same problem, the stock file after a sale is as follows

19
9

9

i made the cnages you siggested by removing the curly braces and the code currently is

[php]

<?php //Warning incase something is out of stock!// $warning= " Please Return to Homepage to cpu Another Selection"; ////////////////Steven Russell////////////////////////////////// // pass variables to be written, open text files, reduce stock, validate////////////////////////// // Set the string to be written to the file//////////////////// $value = "fname: $fname rn"; $value .= "sname: $snamern"; $value .= "E-mail: $email rn"; $value .= "pro: $prorn"; $value .= "cpur: $cpurn"; $value .= "motherb: $motherbrn"; $value .= "Ramtotal: $totrn"; $value .= "Ramcost: $costrn"; $value .= "total cost: $totcostrn"; //Validate to make sure all items are entered. if not exit/// if(($fname=="")||($sname=="")||( $pro=="")||( $motherb=="")||( $tot=="")||( $cost=="")||( $email=="")||( $cpu=="")||( $totcost=="")){ exit; } //////////////// Open files for reading (stock)/////////////// $fp = fopen("stock.txt","rb")or die ("could not open stock file"); $dat = ""; while(!feof($fp)) { $dat.= fgets($fp,4096); } fclose($fp); $val=explode("rn",$dat); ///////////reduce stock levels////////// if ($ram64 == "1") ($val[0] = $val[0]-1); if ($ram128 == "1") ($val[1] = $val[1]-1); if ($ram256 == "1") ($val[2] = $val[2]-1); if ($ram512 == "1")($val[3] = $val[3]-1); if ($ram1024 == "1")($val[4] = $val[4]-1); if ($probath == "1") ($val[5] = $val[5]-1); if ($prohent == "1") ($val[6] = $val[6]-1); if ($motherbbathtot1 == "1") ($val[7] = $val[7]-1); if ($motherbbathtot2== "1")($val[8] = $val[8]-1); if ($motherbhenttot1== "1")($val[9] = $val[9]-1); if ($motherbhenttot2 == "1")($val[10] = $val[10]-1); //////////////out of stock validation////////// if ($val[0] < 1) { $os64 = " 64 MB RAMM is out of stock "; $val[0]="0"; $totcost = $warning ; } if ($val[1] < 1) { $os128 = " 128 MB RAMM is out of stock "; $val[1]="0"; } if ($val[2] < 1) { $os256 = " 256 MB RAMM is out of stock "; $val[2]="0"; $totcost = $warning; } if ($val[3] < 1) { $os512 = " 512 MB RAMM is out of stock "; $val[3]="0"; $totcost = $warning; } if ($val[4] < 1) { $os1024 = " 1024 MB RAMM is out of stock "; $val[4]="0"; $totcost = $warning; } if ($val[5] < 1) { $pro = " $cpu is out of stock "; $val[5]="0"; } if ($val[6] < 1) { $pro = " $cpu is out of stock"; $val[6]="0"; $totcost = $warning; } if ($val[7] < 1) { $motherb = " Motherboard is out of stock "; $val[7]="0"; $totcost = $warning; } if ($val[8] < 1) { $motherb = " Motherboard is out of stock "; $val[8]="0"; $totcost = $warning; } if ($val[9] < 1) { $motherb = " Motherboard is out of stock "; $val[9]="0"; $totcost = $warning; } if ($val[10] < 1) { $motherb = " Motherboard is out of stock"; $totcost = $warning ; $val[10]="0"; } /////////////////assign values for over writing/////////// $val = "$val[0] rn"; $val .= "$val[1]rn"; $val .= "$val[2]rn"; $val .= "$val[3]rn"; $val .= "$val[4]rn"; $val .= "$val[5]rn"; $val .= "$val[6]rn"; $val .= "$val[7]rn"; $val .= "$val[8]rn"; $val .= "$val[9]rn"; $val .= "$val[10]rn"; ////// Open the file for writing/////////////////////// $fp = fopen("stock.txt", "w") or die("Couldn't open stock.txt for writing!"); $numBytes = fwrite($fp, $val ) or die("Couldn't write values to file!"); fclose($fp); $fp = fopen("order.txt", "a") or die("Couldn't open order.txt for writing!"); $numBytes = fwrite($fp, $value) or die("Couldn't write values to file!"); fclose($fp); /////////END OF PHP///////////////// ?> Final Page

 

 

Thank you For your order <?php echo " $fname " ?>

<?php echo " Baseunit : ? 400 " ?>

<?php echo " $cpu processor : ? $pro " ?>

<?php echo " $tot Ram : ? $cost " ?>

<?php echo " $cpu MotherBoard : ? $motherb " ?>

<?php echo " Total Price including all Components : ? $totcost " ?>

The above has been ordered. You will receive an email in the next 24 hours to confirm this.

Email Will be sent to <?php echo "$email"?>

[/php] [b] MOD EDIT: Added PHP bb code tags[/b]

i just dont understand why this is not working :( i am now getting stressed over it. any other suggestions?


#8

Also look at:
[php]

/////////////////assign values for over writing///////////
$val = “$val[0] rn”;
$val .= “$val[1]rn”;
$val .= “$val[2]rn”;
$val .= “$val[3]rn”;
$val .= “$val[4]rn”;
$val .= “$val[5]rn”;
$val .= “$val[6]rn”;
$val .= “$val[7]rn”;
$val .= “$val[8]rn”;
$val .= “$val[9]rn”;
$val .= “$val[10]rn”;
[/php]
You shouldn’t include the ARRAY inside the quotes. Try it like this

[php]/////////////////assign values for over writing///////////
$val = $val[0] .“rn”;
$val .= $val[1].“rn”;
$val .= $val[2].“rn”;
$val .= $val[3].“rn”;
$val .= $val[4].“rn”;
$val .= $val[5].“rn”;
$val .= $val[6].“rn”;
$val .= $val[7].“rn”;
$val .= $val[8].“rn”;
$val .= $val[9].“rn”;
$val .= $val[10].“rn”;
[/php]


#9

yet again thankyou for your time but its still doing the same thing :( would it be possible if i can send you the actual web page that i have done?


#10

Did you try echoing out the $val variable before writing it?

What else have you done to try and debug this.

Everything I have pointed out are potential issues that if not the cause of this problem, are the cause of other (maybe less serrious or more serious) problems.

First check to see if you are actually getting the values in $val that you should before writing to the file.

If you are then the problem is in the WRITE portion, if not then you need to look at the part of the code that generates the value. Honestly, I did not look at the entire code. You need to help us by trying to narrow the problem area down. Basic debugging includes echoing values to the screen to see if they have what you think the have. It seems that you are not doing this.

I don’t want to be a nag, but I also don’t want to write/re-write or debug your code.

What have you done to debug it? Please present that and maybe we can narrow down the problem.


#11

o.k :D i fell that that problem is in these few lines

$fp = fopen("stock.txt", "w") or die("Couldn't open stock.txt for writing!"); 
$numBytes = fwrite($fp, $val ) or die("Couldn't write values to file!"); 

fclose($fp); 

it is suppose to overwrite the values after the php has reduced the stock
[/quote]


#12

Your problem is where you reassign the values to a variable to write to the file.

You are using
[php]

/////////////////assign values for over writing///////////
$val = $val[0] .“rn”;
$val .= $val[1].“rn”;
$val .= $val[2].“rn”;
$val .= $val[3].“rn”;
$val .= $val[4].“rn”;
$val .= $val[5].“rn”;
$val .= $val[6].“rn”;
$val .= $val[7].“rn”;
$val .= $val[8].“rn”;
$val .= $val[9].“rn”;
$val .= $val[10].“rn”;

[/php]

The problem this poses is that you are using a variable with the same name as an array.
So when you do that you just completely changed things. Change it to

[php]

/////////////////assign values for over writing///////////
$val1 = $val[0] .“rn”;
$val1 .= $val[1].“rn”;
$val1 .= $val[2].“rn”;
$val1 .= $val[3].“rn”;
$val1 .= $val[4].“rn”;
$val1 .= $val[5].“rn”;
$val1 .= $val[6].“rn”;
$val1 .= $val[7].“rn”;
$val1 .= $val[8].“rn”;
$val1 .= $val[9].“rn”;
$val1 .= $val[10].“rn”;

////// Open the file for writing///////////////////////
//changed variable name here too.
$fp = fopen(“stock.txt”, “w”) or die(“Couldn’t open stock.txt for writing!”);
$numBytes = fwrite($fp, $val1 ) or die(“Couldn’t write values to file!”);

[/php]


#13

paul buddy, i cant thank you enough, thankyou for taking the time and effort with me to sort out my problem, much appreciated
steven