Jump to content

Recommended Posts

Ive been kind of banging my head against the wall with this one and i cant seem get my head around it. I created a function for parsing a text file, then i put pieces of a row into an array. This is where i get a parse error expecting a ";" message:

 

for ($c=1;i<=$c,$c++){

echo "$pN[$c]";

echo "$pC[$c]";

echo "<img src="$purl[$c]">";

echo "<input type='hidden' name=productName1 value='$pN'>"

echo "<input type='hidden' name=price1 value='$pC[$c]>"

}

 

any help would be appreciated, or if i need to add more info

Link to comment
https://forums.phpfreaks.com/topic/178899-solved-parse-error-in-for/
Share on other sites

Thats what im saying. Im taking a programming class, and this is pretty much what he wrote on the board (Weird writing code in a notebook)

 

im thinking that i might of been a 1 and he just had bad hand writing? but who knows. In the file there is 3 products that look like this:

 

Item$price$Url

Item$price$Url

Item$price$Url

(Amazingly he chose the $ as the delimiter)

 

as for how many times i want it to loop, technically 3 but i think its set up till the end of the file.

P.S.

 

Basically what I hopefuly end up doing is writing a for statement that says "When my counter ( which i set up early in the code by "$c=0") reaches 1, run this code" which will then echo back the product name, product price and display a picture of the product from a given url.

 

I think thats the right set up for it, PHP is just harping on a ";" for some reason =(

the i being a 1 doesn't make any sense either. if thats what your teacher wrote on the board, then you have a terrible teacher.

 

a for loop looks like the following usually

for ($i = 0; $i < Number of times; $i++){

 

but i guess you could also do

for ($i = 1; $i <= number of times; $i++){

 

if you want your loop to run 3 times, then it should be something like

for ($i = 0; $i < 3; $i++);

 

but to be completely honest, none of that really makes sense at all.

 

$pN and $pC seem like arrays, but you access $pN like its a variable. can you post the rest of your code? and perhaps a description of what you want it to do

 

<?php

 

$myfile="Catalog.txt";                                                        //$myfile is now catalog.txt

$fp= fopen($Catalog,'r');                                                        //fh is file handle

$readfile="";                                                                    //reads & stores the entire catalog in $readfile

while(!feof($fp)) {

    $readfile.=fgets($fp,1024);

    $readfile=str_replace("\r","",$Catalog);

    };

 

fclose($fp);                                                                   

 

while ($Catalog!=""){

$delimiter=strpos($Catalog,0,1);

$Catalog=substr($catalog,2);

$c=0;

$endofrow=strpos($Catalog,"$");

$row=substr($Catalog,0,$endofrow);

$Catalog=substr($Catalog,$endofrow+1);

$c++;

$temp=strpos($row,$delimiter);

$pN[$c]=substr($row,0,$temp);

$row=substr($row,$temp+1);

$temp=strpos($row,$delimiter);

$pC[$c]=substr($row,0,$temp);

$row=substr($row,$temp+1);

$purl[$c]+$row;

};

for ($i=1;i<=$c,$c++){

echo "$pN[$c]";

echo "$pC[$c]";

echo "<img src="$purl[$c]">";

echo "<input type='hidden' name=productName1 value='$pN'>"

echo "<input type='hidden' name=price1 value='$pC[$c]>"

}

?>

 

 

 

 

&& now the function page I have but I think the display function might b able to be more or something

 

 

<script type="text/javascript">

function validate()

{

firstName=document.form.firstName.value;

if (firstName=="")

{

alert("Enter First Name");

return false;

}

lastName=document.form.lastName.value;

if (lastName=="")

{

alert("Enter Last Name");

return false;

}

street=document.form.street.value;

if (street=="")

{

alert("Enter Street");

return false;

}

city=document.form.street.value;

if (city=="")

{

alert("Enter City");

return false;

}

zipCode=document.form.zipCode.value;

if (zipCode=="")

{

alert("Enter A Valid Zipcode");

return false;

}

email=document.form.email.value;

posAt=email.indexOf("@");

if (posAt<1)

{

alert("Enter @ in E-mail Please");

return false;

}

 

cost=0;

price1=document.form.price1.value;

quantity1=document.form.quantity1.value;

cost=cost+(price1*quantity1);

price2=document.form.price2.value;

quantity2=document.form.quantity2.value;

cost=cost+(price2*quantity2);

price3=document.form.price3.value;

quantity3=document.form.quantity3.value;

cost=cost+(price3*quantity3);

price4=document.form.price4.value;

quantity4=document.form.quantity4.value;

cost=cost+(price4*quantity4);

if (cost==0)

{

alert("Please Make A Purchase to Continue");

return false;

}

productName1=document.form.productName1.value;

productName2=document.form.productName2.value;

productName3=document.form.productName3.value;

productName4=document.form.productName4.value;

totalCost="Your Final Purchase Totals $" +cost;

if(confirm(totalCost)){

document.form.submit();

return true;

}

else

{

return false;

}

}

</script>

 

Here is for the form below:

 

 

</table>

<table>

<tr>

<td><b>Fill Out The Form:</b></td>

</tr>

<tr>

<td><b>First Name:</b></td>

<td><input type="text" size="20" maxlength="40" name="firstName" /></td>

</tr>

<tr>

<td><b>Last Name:</b></td>

<td><input type="text" size="20" maxlength="40" name="lastName" /></td>

</tr>

<tr>

<td><b>Street:</b></td>

<td><input type="text" size="30" maxlength="40" name="street" /></td>

</tr>

<tr>

<td><b>City:</b></td>

<td><input type="text" size="20" maxlength="40" name="city" /></td>

</tr>

<tr>

<td><b>Zipcode:</b></td>

<td><input type="text" size="20" maxlength="40" name="zipCode" /></td>

</tr>

<tr>

<td><b>E-mail:</b></td>

<td><input type="text" size="20" maxlength="40" name="email" /></td>

</tr>

<tr>

<td><input type="button" value="submit" onclick="validate()"></td>

</tr>

</table>

</form>

</body>

</html>

 

 

Essentially i would like to create a webpage that displays the name price and picture from a url using php. I was given the task of reading a file, getting the delimiter, parse and display the file. Dont mind the javascript, im just worried about this php part.

firstly, post your code in code tags. also you dont need to post html or javascript if you have a php problem.

 

but the first thing that caught my eye

$myfile="Catalog.txt";                                                        //$myfile is now catalog.txt
$fp= fopen($Catalog,'r');  

$Catalog doesn't exist. perhaps you meant

 

$myfile="Catalog.txt";
$fp= fopen($myfile,'r');

 

the following

$myfile="Catalog.txt";                                                        //$myfile is now catalog.txt
$fp= fopen($Catalog,'r');                                                        //fh is file handle
$readfile="";                                                                     //reads & stores the entire catalog in $readfile
while(!feof($fp)) {
    $readfile.=fgets($fp,1024);
    $readfile=str_replace("\r","",$Catalog);
    };

should probably be something like

$myfile="Catalog.txt";                                                        //$myfile is now catalog.txt
$fp= fopen($myfile,'r');                                                        //fh is file handle
$readfile="";                                                                     //reads & stores the entire catalog in $readfile
while(!feof($fp)) {
    $readfile.=fgets($fp,1024);
    $readfile=str_replace("\r","",$readfile);
    }

 

also you don't need a semi colon after closing brackets like you have in your while loop

 

another thing, this doesn't make sense

 

while ($Catalog!=""){
      $delimiter=strpos($Catalog,0,1);
      $Catalog=substr($catalog,2);
      $c=0;//you should create this loop variable outside the loop.
//every time the loop gets run, it gets overwritten.
      $endofrow=strpos($Catalog,"$");
      $row=substr($Catalog,0,$endofrow);
      $Catalog=substr($Catalog,$endofrow+1);
      $c++; //are you sure you want to update your loop variable here?
//usually you update loop variables at the end of the loop
      $temp=strpos($row,$delimiter);
      $pN[$c]=substr($row,0,$temp);
      $row=substr($row,$temp+1);
      $temp=strpos($row,$delimiter);
      $pC[$c]=substr($row,0,$temp);
      $row=substr($row,$temp+1);
      $purl[$c]+$row;
};//you dont need that semi colon, as I stated above

$Catalog isn't defined. What kind of errors are you getting? you must be getting quite a few syntax and logical errors

 

maybe it should look something like this

$c=0;
while ($Catalog!=""){
      $delimiter=strpos($Catalog,0,1);
      $Catalog=substr($catalog,2);
      $endofrow=strpos($Catalog,"$");
      $row=substr($Catalog,0,$endofrow);
      $Catalog=substr($Catalog,$endofrow+1);
      $temp=strpos($row,$delimiter);
      $pN[$c]=substr($row,0,$temp);
      $row=substr($row,$temp+1);
      $temp=strpos($row,$delimiter);
      $pC[$c]=substr($row,0,$temp);
      $row=substr($row,$temp+1);
      $purl[$c]+$row;
      $c++;
}

 

but to be completely honest, I still have no clue what this is supposed to accomplish, and whatever it is, it doesn't appear as if it would be.

 

Can you describe the errors your getting?

 

 

 

Sorry, what are code tags?

And thus far i've gotten to this error:

            Parse error: parse error, expecting `','' or `';'' in C:\wamp\www\Project3WEB.php on line 32

 

which happens to be:

echo "<img src="$purl[$c]">";

 

im only getting 1 error everytime i fix something. Not sure if its supposed to work that way?

yes it is. once PHP comes upon an error, it stops and tells you the error. code tags are just that. tags to put code in. you can use [ code ] [/ code ] for general code, or [ php ] [/ php ] for php tags (without the spaces of course)

 

the line you specified,

echo "<img src="$purl[$c]">";

 

when you have double quotes surrounding a string, you have to either escape double quotes that are inside the strings, or use single quotes, like so

echo "<img src=\"$purl[$c]\">";//escape quotes
echo "<img src='$purl[$c]'>";//single quotes

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.