garfee Posted March 3, 2009 Share Posted March 3, 2009 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/147822-solved-nested-php-issue/ Share on other sites More sharing options...
premiso Posted March 3, 2009 Share Posted March 3, 2009 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>'; } Should fix you up. Quote Link to comment https://forums.phpfreaks.com/topic/147822-solved-nested-php-issue/#findComment-775899 Share on other sites More sharing options...
ricmetal Posted March 3, 2009 Share Posted March 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/147822-solved-nested-php-issue/#findComment-775903 Share on other sites More sharing options...
garfee Posted March 4, 2009 Author Share Posted March 4, 2009 '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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147822-solved-nested-php-issue/#findComment-775924 Share on other sites More sharing options...
garfee Posted March 4, 2009 Author Share Posted March 4, 2009 Thanks Premiso and Ricmetal. Problem solved. Sometimes I surprise myself with my own stupidty !!! Cheers guys. Quote Link to comment https://forums.phpfreaks.com/topic/147822-solved-nested-php-issue/#findComment-775944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.