EternalSorrow Posted August 15, 2009 Share Posted August 15, 2009 I've been searching for an answer to a question I have. I know PHP can be embedded within itself, but I'm curious if an IF statement can be the code embedded into the larger PHP statement. Does anyone know of a way to do this? Here is the example code I'm playing with: echo '<h3>'.$title.'<?php if($session->logged_in){ echo " - <a href="edit.php?cmd=edit&title='.$title.'">Edit</a>"; }; ></h3>'; Link to comment https://forums.phpfreaks.com/topic/170343-solved-php-within-php-if-statement/ Share on other sites More sharing options...
trq Posted August 15, 2009 Share Posted August 15, 2009 echo '<h3>' . $title . ($session->logged_in) ? ' - <a href="edit.php?cmd=edit&title=' . $title . '">Edit</a>' : '' . '</h3>'; Link to comment https://forums.phpfreaks.com/topic/170343-solved-php-within-php-if-statement/#findComment-898585 Share on other sites More sharing options...
roopurt18 Posted August 15, 2009 Share Posted August 15, 2009 I know PHP can be embedded within itself... You are misunderstanding something about PHP because that statement makes little logical sense. Perhaps if you clarify what you meant we can steer you in the right direction. Link to comment https://forums.phpfreaks.com/topic/170343-solved-php-within-php-if-statement/#findComment-898586 Share on other sites More sharing options...
EternalSorrow Posted August 15, 2009 Author Share Posted August 15, 2009 @thorpe I'm afraid the code you gave me doesn't work for some reason. The logged_in portion does appear, but the $title field at the head does not. I've tried numerous ways of adjusting it, but all end in either error or still a blank area where the $title should be. @roopurt18 I know PHP can be embedded within itself... You are misunderstanding something about PHP because that statement makes little logical sense. Perhaps if you clarify what you meant we can steer you in the right direction. If I am misunderstanding something about PHP, I do wish you'd tell me what this is. Your remark is cryptic and, to put it frank, I have no idea what you're talking about nor asking. Link to comment https://forums.phpfreaks.com/topic/170343-solved-php-within-php-if-statement/#findComment-898589 Share on other sites More sharing options...
trq Posted August 15, 2009 Share Posted August 15, 2009 @thorpe I'm afraid the code you gave me doesn't work for some reason. The logged_in portion does appear, but the $title field at the head does not. I've tried numerous ways of adjusting it, but all end in either error or still a blank area where the $title should be. @roopurt18 I know PHP can be embedded within itself... You are misunderstanding something about PHP because that statement makes little logical sense. Perhaps if you clarify what you meant we can steer you in the right direction. If I am misunderstanding something about PHP, I do wish you'd tell me what this is. Your remark is cryptic and, to put it frank, I have no idea what you're talking about nor asking. He is talking about the fact that you can't embed php code within a string and expect it to execute. I see nothing logically wrong with my code, where is $title defined? Can we see the html produced by my code? Link to comment https://forums.phpfreaks.com/topic/170343-solved-php-within-php-if-statement/#findComment-898590 Share on other sites More sharing options...
EternalSorrow Posted August 15, 2009 Author Share Posted August 15, 2009 I use the word 'embed' here because I can think of no better word which describes placing one object within another, without of course using that long string of words from it's definition. And here's the entirety of the code. By the length you can tell why I wish to, um, place the logged_in statement within the echo as otherwise I would be forced to duplicate this twice on the same page, one for logged_in users to view and another for guests. <?php mysql_connect(localhost,user,pw); @mysql_select_db(db) or die( "Unable to select database"); $query=sprintf("SELECT * from fanfiction WHERE `story`= '%s' ORDER BY id asc ", mysql_real_escape_string($story)); $result = mysql_query( $query ) or die(mysql_error()); $screen = $_GET['screen']; $PHP_SELF = $_SERVER['PHP_SELF']; $rows_per_page=1; $total_records=mysql_num_rows($result); $pages = ceil($total_records / $rows_per_page); $last = $pages -1; if (!isset($screen)) $screen=0; $start = $screen * $rows_per_page; $query .= "LIMIT $start, $rows_per_page"; $result= mysql_query($query) or die ("Could not execute query : $query." . mysql_error()); while ($row = mysql_fetch_array($result)) { extract($row); echo '<h3>' . $title . ($session->logged_in) ? ' - <a href="edit.php?cmd=edit&title=' . $title . '">Edit</a>' : '' . '</h3>'.$chapter.''; } echo '<ul class="series">'; if ($screen == 0) { } else { echo "<li><a href=\"$PHP_SELF?story=$story&screen=0\">First</a>"; } // Put Code C Here // create the dynamic links if ($screen > 0) { $j = $screen - 1; $url = "$PHP_SELF?story=$story&screen=$j"; echo "<li><a href=\"$url\">Previous</a>"; // } // page numbering links now $p = 3; // number of links to display per page $lower = $p; // set the lower limit to $p $upper = $screen+$p; // set the upper limit to current page + number of links per page while($upper>$pages){ $p = $p-1; $upper = $screen+$p; } if($p<$lower){ $y = $lower-$p; $to = $screen-$y; while($to<0){ $to++; } } if(!empty($to)) { for ($i=$to;$i<$screen;$i++){ $url = "$PHP_SELF?story=$story&screen=" . $i; $j = $i + 1; echo "<li><a href=\"$url\">$j</a>"; } } for ($i=$screen;$i<$upper;$i++) { $url = "$PHP_SELF?story=$story&screen=" . $i; $j = $i + 1; echo "<li><a href=\"$url\">$j</a>"; } if ($screen < $pages-1) { $j = $screen + 1; $url = "$PHP_SELF?story=$story&screen=$j"; echo "<li><a href=\"$url\">Next</a>"; } if ($screen == $last) { } else { echo "<li><a href=\"$PHP_SELF?story=$story&screen=$last\">Last</a>"; } ?> Link to comment https://forums.phpfreaks.com/topic/170343-solved-php-within-php-if-statement/#findComment-898595 Share on other sites More sharing options...
trq Posted August 15, 2009 Share Posted August 15, 2009 Ok, turns out I did have a logical issue. echo ($session->logged_in) ? "<h3>{$title} - <a href='edit.php?cmd=edit&title={$title}'>Edit</a></h3>{$chapter}" : "<h3>{$title}</h3>{$chapter}"; Link to comment https://forums.phpfreaks.com/topic/170343-solved-php-within-php-if-statement/#findComment-898599 Share on other sites More sharing options...
EternalSorrow Posted August 15, 2009 Author Share Posted August 15, 2009 That worked perfectly! Thanks for your help! I'll be sure to study the PHP for this statement, as I've numerous cases where this can be used. Link to comment https://forums.phpfreaks.com/topic/170343-solved-php-within-php-if-statement/#findComment-898603 Share on other sites More sharing options...
roopurt18 Posted August 15, 2009 Share Posted August 15, 2009 @roopurt18 I know PHP can be embedded within itself... You are misunderstanding something about PHP because that statement makes little logical sense. Perhaps if you clarify what you meant we can steer you in the right direction. If I am misunderstanding something about PHP, I do wish you'd tell me what this is. Your remark is cryptic and, to put it frank, I have no idea what you're talking about nor asking. I'll try and clarify with an analogy. I know computers can be embedded within themselves. Does that statement make sense? According to Joe and how he understands computers, it must make perfect sense. But if you are like me and have a pretty extensive knowledge of computers, then you are left asking yourself, "What in the Hell is Joe talking about?" Joe has obviously misunderstood something about computers because he is making statements that do not make logical sense. But how am I to know what Joe has misunderstood? Should I give Joe a history of computers, their development, their components, the software they run, etc? That could take weeks! I have a fairly good understanding of programming languages and to say: I know you can embed <random language> into itself. Is highly ambiguous and can mean a myriad of things. So based on your original statement, which is ambiguous, can be interpreted in who knows how many different ways, and doesn't make sense: I know PHP can be embedded within itself... The only concrete thing I am able to tell you is: You have misunderstood something about PHP. I have no idea what it is you have misunderstood because your statement doesn't make any sense (it's equivalent to dividing by zero). But perhaps if you clarify what you mean by "embedding PHP in itself", I can determine what exactly you have misunderstood and I can help you understand a particular PHP concept better. I don't know how to be any more clear than that. Link to comment https://forums.phpfreaks.com/topic/170343-solved-php-within-php-if-statement/#findComment-898632 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.