Jump to content

[SOLVED] Nested PHP issue


garfee

Recommended Posts

I have a php string that I want to display if a user has logged in to my website. I cannot get it to work however. I think it is because I have php embedded within php tags. The code is below. Does anyone know how I could re arrange this to get it to work? Many thanks.

 

<?

 

if($logged_in){

  echo '<a href="tex.php?id=<?PHP echo $rows['id'] ?>"><?PHP echo $rows['title']." - <i>".$rows['author']."</i>" ?></a>';

}else{

  echo '<p class="logintext"><a href="main.php">Login / Register</a></p>';

 

?>

Link to comment
https://forums.phpfreaks.com/topic/147822-solved-nested-php-issue/
Share on other sites

when you echo something and insert php in the middle of the echo you have to end the echo where php code restarts.

 

something like that.

 

eg

 

$a = "world";

echo "hello ".$a.", nice to meet you!";

 

the dot is called conctacnation (something!) meaning "attaching"

so youll attaching ehcoed stuff to php stuff to form a big non php line (usually html)

 

echo "not php".$php."not php";

 

more so:

 

echo "<td id='bgColor' style='width: 10%'><a href='add.php?id=".$info['id']."'>;

 

notice the end of the above line

 

"'>;

 

it's

 

" + '>;

 

edit:

ill leave the post, better to teach a man to fish than to give him food ei

besides all this writing isnt going to waste

'Should fix you up.'

 

Thanks for your reply Premiso. I changed the code but when I do it does not build the string including the variables. ie. I only get the hyphen and when hovered over it does not add the id number to the link. So basically my link looks like this  - 

 

Here is the entire code:

 

<?

/* Include Files *********************/

session_start();

include("database.php");

include("login.php");

/*************************************/

?>

 

 

<?php

 

$host="localhost";

$username="username";

$password="password";

$db_name="db";

$tbl_name="tbl";

 

 

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

 

$sql="SELECT * FROM $tbl_name ORDER BY author DESC";

$result=mysql_query($sql);

 

 

while($rows=mysql_fetch_array($result)){

?>

 

 

 

 

<?

 

}

 

 

mysql_close();

?>

<?

if($logged_in){

  echo '<a href="tex.php?id=' . $rows['id'] . '">' . $rows['title'] . ' - <i>'.$rows['author'].'</i></a>';

}else{

  echo '<p class="logintext"><a href="main.php">Login / Register</a></p>';

}

 

?>

 

 

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.