Parse Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Parse Error

Post by crazycaddy »

I'm stumped with this code:

Code: Select all

$delete = if($_SESSION['permission']==yes){ echo '<a href=\"proe/info/delete.php?name=$name\">-Delete</a>'; } else { };
I keep getting a parse error and i'm not sure why, ive tried playing around with the ' and " and also using \ in different places but no luck :cry:
Anyone have a suggestion? Or even better an answer to this problem :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

i dont think you can assign a variable to a if conditional

Code: Select all

$delete = ($_SESSION['permission']==yes) ?  '<a href=\"proe/info/delete.php?name=$name\">-Delete</a>' : '');
echo $delete;
try that
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

nope still a parse error. I should of really mentioned that it is posting to a file, and I want it to check for the sessions when the file is displayed, ive got this work with this code:

Code: Select all

$delete = "if ($ad){";
$del1 = "}";
$content = "<TR><TD><font face=verdana size=1px>$utilid</font></TD><TD><font face=verdana size=1px>$utilname</font></TD><TD><font face=verdana size=1px>$utildes</font></TD><TD><font face=verdana size=1px><a href='$utilurl' target='blank'>$utilurl</a></font></TD><? $delete ?><TD border='0'><font face=verdana size=1px><a href=\"util.php?action=del&ID=$utilid&URL=$utilurl\">delete</a></TD></TR><? $del1?>
";
Now that works fine, its just when i check for sessions I get the parse errors :?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

crazycaddy wrote:

Code: Select all

$delete = "if ($ad){";
$del1 = "}";
$content = "<TR><TD><font face=verdana size=1px>$utilid</font></TD><TD><font face=verdana size=1px>$utilname</font></TD><TD><font face=verdana size=1px>$utildes</font></TD><TD><font face=verdana size=1px><a href='$utilurl' target='blank'>$utilurl</a></font></TD><? $delete ?><TD border='0'><font face=verdana size=1px><a href="util.php?action=del&ID=$utilid&URL=$utilurl">delete</a></TD></TR><? $del1?>
";
i have no idea what you are doing there. if you want to check for a session do somtin like

Code: Select all

echo (false !== isset($_SESSION['permission']) ? '<a href="delete.php">Delete</a>' : '');
dont assign that to a variable or anything just put that code in.

but the code you said works...what are you doing with that? that really does not much any sence.
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

its an odd way of doing things but, what it does is add a table row html code (<tr> etc) to utillist.php and another page displays it. I dont want the session to be checked in that code, but rather in utillist.php.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

$delete = ($_SESSION['permission'] == 'yes' ?  '<a href="proe/info/delete.php?name=$name">-Delete</a>' : '');
sorry the original code was missing the '' around the yes
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

well that works (writes to the file successfully) but it parses during the 'writing to the file' so the delete link will ALWAYS show as it was posted by someone whos session was authenticated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why dynamically create a file when you could dynamically be the file?
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

Ive done that on some of the scripts, you see ive had the task of creating a whole intranet without the aid of mysql. This way seemed to be the simplest, but I guess I was wrong :roll: . Ill try now just exploding text files with a similar format to : bob | jim etc.
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

OK I came up with this:

Code: Select all

<?php
$data = file('downloads/util/utillist.php');
$data = array_reverse($data);
foreach($data as $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
    echo '<tr><td><a href=\"$pieces[2]\">$pieces[0]</a></td><td>$pieces[1]</td><td>Delete</td>';
}
?>
But it justs prints $pieces[2] etc rather than parsing. Ive tried using the php tags around them but I just get parse errors :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're using a single quote string. Single quote strings do not parse any variables contained inside them.
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

Thanks that worked (but im sure I tried using " " earlier), but this just keeps getting better :evil: . How can I check for the session in here without causing a parse error:

Code: Select all

echo "<tr><td><a href=\"$pieces[2]\">$pieces[0]</a></td><td>$pieces[1]</td><td>--Here--</td>";
heres the code i want to 'impliment' :

Code: Select all

if($_SESSION['permission']==yes){
echo'<a href=\"downloads/util/delete.php?name=$pieces[0]\">';
}
else {}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

options:
  • check to see if $pieces is empty, if so, fill it with enough empty elements
  • check if $pieces is empty inside the if, if so, output something different.
you may need to check the individual elements of $pieces too, so be careful..
crazycaddy
Forum Commoner
Posts: 43
Joined: Fri Aug 05, 2005 6:59 am
Location: England, UK

Post by crazycaddy »

I found a workaround,





but guess what?




more problems :roll: .

before I used to write to the file very 'dodgeleleley' as i opened it with W but it didnt wipe the whole file each time, it just added it at the end. But now its decided to work :cry: . I tried changing the variable to 'a' but it doesnt write anymore.

Code: Select all

<?php
$name = $_POST['name'];
$des = $_POST['des'];
$url = $_POST['url'];
$content = "$name|$des|$url"; 
$file = fopen("utillist.php", "r");
$read = fread($file, filesize("utillist.php"));
fclose($file);
$blah = fopen("utillist.php", "a");
fwrite($blah, "$content $read");
fclose ($blah);
print'<META HTTP-EQUIV="Refresh" CONTENT="2; URL=../../index.php?id=1">';
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code reads the entire file and prepends the addition.. why not continue using 'w' ?
Post Reply