PHP - If Statement Inside An Echo?
Hello guys im new to php i been coding for like a week now, i need some help here i have been stuck for like 6 hours XD,is it possible to write an IF Statement inside an echo?
//this is what i want to do // echo a table and a delete button // and if you click on the delete button it echoes out "DELETED" echo " <table width='528px'> <tr> <td> </td> <td> <center><font size='5'>$tittle</font></center><br> </td> </tr> <tr> <td> </td> <td> $message </td> </tr> <tr> <td> </td> <td> <font size='1'>Posted By:<font color='green'>$author</font> on <font color='gray'>$date</font> at <font color='gray'>$time</font></font> <input id='delete' name='delete' type='submit' value='Delete' > if ($_POST['delete']) { echo "DELETED"; } <td> </td> </tr><br><br> </table> "; Similar TutorialsHi I am new to PHP and this is my first post one here so appologies is this questions seems a bit dumb! I have an if clause such that if a button is pressed on my web page then i want to reload the page and include a new form on it. I am having a problem getting the $_SERVER['PHP_SELF'] command to work from iside a echo command. I must not be escaping the code correctly with back slashes: I currently have the line : echo"<form method=\"POST\" action=\"\<?$_SERVER['PHP_SELF']?\>\">"; However this doesnt seem to work as my page just doesnt display in the browser. Any advice is much appreciated. Thanks for taking the time to read. I'm having issues with the following: Code: [Select] <?php session_start(); $_SESSION['username']=$_POST['username']; $_SESSION['password']=$_POST['password']; if($_SESSION['username']=="username" && $_SESSION['password']=="password"){ if($_GET['product']=="add"){ $content.=' <p><label>Product Name:</label> <input type="text" name="product_name" size="30" /> <label>Product Price:</label> <input type="text" name="product_price" size="5" /> </p> <p><label>Product Category:</label> <input type="text" name="product_category" size="30" /></p> <p><label>Product Link:</label> <input type="text" name="product_link" size="30" /></p> <p><label>Product Image:</label> <input type="text" name="product_image" size="30" /></p> <p><label>Product Tag:</label> <input type="text" name="product_tag" size="30" /></p> <p><label>Product Keywords:</label> <input type="text" name="keyword" size="30" /></p> <p><label>Product Features:</label><br /> <textarea name="product_features" rows="10" cols="60"></textarea> </p> <p><label>Product Pros:</label><br /> <textarea name="product_pros" rows="5" cols="30"></textarea> </p> <p><label>Product Cons:</label><br /> <textarea name="product_cons" rows="5" cols="30"></textarea> </p> <p><label>Product Description:</label><br /> <textarea name="product_description" rows="10" cols="60"></textarea> </p> <p><label>Product Notes:</label><br /> <textarea name="product_notes" rows="5" cols="30"></textarea> </p> '; $logout='<div><a href="./acp_admincp.php?log-out">Log-Out</a></div>'; } elseif($_GET['product']=="view"){ } else{ $content.=' <a href="./admincp.php?product=add">Add New Product</a> <br /> <a href="./admincp.php?product=view">View Products</a> '; } } elseif(isset($_GET['log-out'])){ session_start(); session_unset(); session_destroy(); header("Location: ./admincp.php"); } else{ $content=' <form action="./admincp.php" method="post"> <p><label>Username:</label> <input type="text" name="username" size="30" />'; $content.='</p> <p><label>Password:</label> <input type="password" name="password" /></p>'; $content.='<p><input type="submit" value="Submit" name="Submit" /></p> </form>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <base href="http://ghosthuntersportal.com/" /> <title>Ghost Hunter's Portal - Admin Control Panel</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="verify-v1" content="" /> <meta name="keywords" content="ghost, hunters, hunter, ghosts, spirit, spirits, paranormal, investigation, investigator, investigators, k2, emf, meter, kii" /> <meta name="description" content="Ghost Hunters Potal. Parnormal research equipment store." /> <meta name="author" content="Andrew McCarrick" /> <meta name="robots" content="index, follow" <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <img src="./logo.png" alt="Ghost Hunter's Portal Admin Control Panel" /> <br /> <div style="color: #AA44AA; font-size: 26px; margin-top: -30px; margin-left: 125px;">Admin Control Panel</div> <?php echo $logout; echo $content; ?> </body> </html> I can log-in, and get to the page with the two links on it. However, once I click one of the links it falls back to the log-in page, and it ends up being a never ending loop. It's doing this: Log-In --> Page with links ---> Log-In page again Should be doing this: Log-In --> Page with links --> Add Product page or View Products page I can never get into the the actual sub page. Just to be clear, the address bar actually shows product=add or product=view, but it still shows the log-in page. Welcome member. I am able to get data added to my db and when it gets to the welcome page I want it to welcome $firstname but it comes up blank. How can I fix my echo statement. <?php session_id(); session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome</title> </head> <body> <?php /* Program: New_member.php * Desc: Displays the new member welcome page. Greets * member by name and gives a choice to enter * restricted section or go back to main page. */ if (@$_SESSION['auth'] != "yes") @include('Connections/connect_to_mysql.php'); $result = mysql_query("SELECT firstname FROM `Members` WHERE id='{$_SESSION['id']}'"); $row = mysql_fetch_array($result); echo "<html> <head><title>New Member Welcome</title></head> <body> <h2 style='margin-top: .7in; text-align: center'> Welcome, $firstName </h2>\n"; ?> <p>Your new Member accounts lets you enter the members only section of our web site. You'll find special discounts, a profile of matches, live advise for experts, and much more.</p> <p>Your new Member ID and password were emailed to you. Store them carefully for future use.</p> <div style="text-align: center"> <p style="margin-top: .5in; font-weight: bold"> Glad you could join us!</p> <form action="profile.php" method="post"> <input type="submit" value="Enter the Members Only Section"> </form> <form action="index.php" method="post"> <input type="submit" value="Go to Main Page"> </form> </div> </body> </html> I am having a problem echoing a line of code. I am doing this on my local machine. Anyway, here are the to files involved... home.php <?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; include $_SERVER['DOCUMENT_ROOT'] . '/includes/doctype.inc.php'; include $_SERVER['DOCUMENT_ROOT'] . '/includes/head.inc.php'; ?> </head> <body> <div id="wrapper"> <!-- start horizontal navigation --> <?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/topnav.inc.php'; ?> <!-- end horizontal navigation --> <div id="content"> <h1><?php echo htmlout($cattitle); ?></h1> <span class="desc"><?php echo ($catdesc); ?></span> <?php include $_SERVER['DOCUMENT_ROOT'] . '/info.php'; ?> </div> <!-- start footer --> <?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.inc.php'; ?> <!-- end footer --> </div> </body> </html> and info.php <?php $result = mysqli_query($link, 'SELECT * From includes where catid ='.$_GET['id']); if (!$result) { $error = 'Error getting categories: ' . mysqli_error($link); include 'error.php'; exit(); } while ($row = mysqli_fetch_array($result)) { $inc[] = array('id' => $row['id'], 'include' => $row['include'], 'inc_orderby' => $row['inc_orderby'], 'inc_avail' => $row['inc_avail']); } foreach ($inc as $include): { echo "include $_SERVER['DOCUMENT_ROOT'] . '/includes/' . $row['include'];"; } endforeach; ?> I can't get the echo statement at the bottom of info.php echo "include $_SERVER['DOCUMENT_ROOT'] . '/includes/' . $row['include'];"; to show up correctly on the home.php page in this spot. include $_SERVER['DOCUMENT_ROOT'] . '/info.php'; what i am attempting to do is loop through some additional information relevant to the page a user selects on the site and display it. (I still have to set up some qualifying statements but this was just the first step to see if anything would show up) This is the parse error I get. Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\info.php on line 18 I've tried messing with the quotes in the echo statement cause I'm sure they aren't right but I never came up with a quote combination that worked. I'm assuming that is the problem. Anyway thanks for taking a look. So I need to echo a row from my database with php, but where i need to echo is already inside an echo. This is my part of my code: $con = mysql_connect("$host","$username","$password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("main", $con); $result = mysql_query("SELECT * FROM Vendor"); while($row = mysql_fetch_array($result)) { //I need to echo right here .................. but I get a blank page when I try this. Please Help. echo '<option value=$row['vendor_id']>'; echo $row['vendor_id']; echo '</option>'; } mysql_close($con); Result: A Blank page. Thanks in advance! Is it possible to echo php inside php? Im guessing not because you are already inside php but there must be a way round my problem. What I would like to do is if the url has project = something then echo <?php require "footer.php"; ?> if not dont echo anything. What is the best way to go about doing this ? how can i put an if statement inside an echo this is what i want to do Code: [Select] <?php echo "<li> <a class='thumb' href='../images/Sections/pinkpanthers/" . $year . "/" . $sessions . "/" . $day . "/" . $y . ".jpg' title=''> <img src='../images/Sections/pinkpanthers/" . $year . "/" . $sessions . "/" . $day . "/thumbs/" . $y . ".jpg ' /></a><div class='caption'> <div class='download'><a href='../images/Sections/pinkpanthers/" . $year . "/" . $sessions . "/" . $day . "/big/" . $y . ".jpg ' target='_blank' />Download</a> </div><div class='image-desc'>" . if ($count == 1){echo "<a href='tag.php?tag=$name'" . $name . ">" . $name . "</a>";}if ($count > 1){$z = 0;$w = $count - 1;while ($z <= $w){$p = $multi[$z];echo "<a href='tag.php?tag=$p'" . $p . ">" . $p . "</a>";$z++;}} . "</div></div></li>"; ?> all the code works wright up until i added the if statements in the last little bit Hello, I need to add rel="..." in the first part of the script but it is not working, below is the script: Code: [Select] echo " <a href='products/".$image[$abc]."' target='_blank'><img border=0 src='products/thumbs2/".$image[$abc]."' /></a>"; Thank you, Hello,
I am working on this function that echo's out images from my database restricted to 10 images with 4 default images if the database is empty.
<?php $query = mysql_query('SELECT image_id, image_expiry FROM images ORDER BY RAND() LIMIT 10'); $i = 0; class myCounter3 implements Countable { public function count() { static $count = 0; return ++$count; } } $counter = new myCounter3; while ($row = mysql_fetch_array($query)) { if($i % 10 === 0) { } echo '<img src="http://www.mysite.com/'.$row['image_id'].'.jpg" width="300" height="auto"/>'; $i++; } for (; $i <= 4; $i++) { echo '<img src="http://www.mysite.com/default.jpg" width="300" height="auto"/>' } ?>I need to include an image expiry function with the function above, but I am not sure how to properly include it. Here is my planned expiry function (each image as an expiry date). $today = date("Y-m-d", time()); $expiry = $row['image_expiry'] if( $today > $expiry) { ## ignore expired image } else { ## display image }Bottom line I need to exclude expired images from being echoed out in the first function. Thanks in advance. Everything I have tried hasn't worked. I want to add a space after $item in the below statement so that there is more space between the output of Item and Qty. I am using the Twitch API to tell if a user is live. I'm new to the Twitch API and PhP so please forgive me if I make any mistakes. Expand the Spoiler tags below to see the code. The code below SHOULD be telling me if a channel is not live or not. It tells me if they are live, but not if they are not live. I don't understand.
Spoiler
<?php if (!empty($_GET['channel'])) { $channel = $_GET['channel']; $dataArray = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?channel='.$channel), true); foreach ((array) $dataArray['streams'] as $mydata) { if (!empty($mydata['_id'])) { echo "Channel is live<br><br>"; $uptime = $mydata['created_at']; $uptime = str_replace("Z", "UTC", $uptime); $uptime = preg_replace("/T/", "", $uptime, 1); echo "Twitch Uptime: ".$uptime."<br />"; date_default_timezone_set('UTC'); $localUptime = date('Y-m-dh:i:sT', time()); echo "UTC Local Time: ".$localUptime; } else { echo "Channel is not live"; } } } else { echo "No channel"; } ?> Hey, I posted a thread before about a news cms I've been developing. And right now I'm trying to flesh out the css of the articles I post on the home page. Now the problem I having is everytime I place a <span class=""></span> or <p class=""></p> or even <div class=""></div>, my web page does NOT load, and instead gives me an error. Code: [Select] Parse error: syntax error, unexpected T_STRING in /home/content/d/j/s/djsmiley/html/index.php on line 405 Now, I know it's possible to add css to a PHP echo statement, but I'm clearly doing something wrong. Here's the code for the php echo statement with the css included: Code: [Select] <? include("dbconnect.php"); //include the file to connect to the database $getnews = mysql_query("SELECT * FROM mynews ORDER BY id DESC"); //query the database for all of the news while($r=mysql_fetch_array($getnews)){ //while there are rows in the table extract($r); //remove the $r so its just $variable echo("<br><span class="NewsID">$type</span> <span class=h2>$title</span><br><br> <em>posted by <strong>$user</strong> | added on $time</em><br><br> $message<br><br> <label class="fltlft2"><img src="../../Websites/DJSmiley.Net/images/Icons/Arrows/Right.png" width="20" height="20"/></label><a href=$url>Read more - $url</a> <div class="newsLikeShareRate"> <table width="100%" border="0"> <tr> <td width="3%" height="21"><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script> <fb:like href="$url" show_faces="true" width="450" font="arial"></fb:like> </td> <td width="65%"><a name="fb_share" id="fb_share4" type="icon_link" share_url="$url">Share</a> <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></td> <td width="32%">Rate this article: </td> </tr> </table> </div>"); } ?> If you're wondering why there's a facebook script in there, I made it to where everytime a new post is added, a facebook like and share button are added, and are assigned the url I specify when the article is posted. Well, I think that's it for the code. Can anyone tell me what I'm doing wrong? Im having a problem with getting the quotes correct with this. I can either get the variable to work in the href and img src or in the css. Anyone have any ideas on this. Cuase im really stuck. This allows the $address and $cos to echo in but not the css variables <td><?php echo "<a href='planet_profile.php?planet=$address'> <img src='images/star.jpg' id='$cos' style='position:absolute;' left:'$b_x px;' top:'$b_y px;'></a>"; ?></td> and this allows only the css variables to work <td><?php echo '<a href="planet_profile.php?planet="' . $address . '"> <img src="images/star.jpg" id="' . $cos . '" style="position:absolute; left:' . $b_x . 'px; top:' . $b_y . 'px;"></a>'; ?></td> Currently whether data from the queries are found or not, its surrounded inside the div tags... i want to somehow echo the div tags inside the while loop, but only echo once. the confusing part for me is that the queries are connecting to 2 different tables for data.. and both need to be found for the div to be echo. if($item['quantity'] >= 1){ if($item2['type'] == 'weapon'){ code below.... Code: [Select] <div class="g_content"><h3 style="text-align: left"> Weapons</h3><div class="g_text"> <table align='center' cellspacing='10'> <?php $current_col = 1; $max_col = 4; $query = $db->execute("select * from items where `player_id`=?", array($player->id)); while($item = $query->fetchrow()) { $query2 = $db->execute("select * from `blueprint_items` where `id`=?",array($item['item_id'])); $item2 = $query2->fetchrow(); //DISPLAY IF QUANTITY IS 1 OR MORE. if($item['quantity'] >= 1){ if($item2['type'] == 'weapon'){ //Open new row if first column if($current_col==1) { echo "<tr>\n"; } //Display current record echo "<td width='25%'>"; echo "<center><img src=\"{$item2['img']}\" width='80' height='80' style=\"border: 1px solid #CC9900\"></center>"; echo "<center><a href=\"../description.php?id={$item2['id']}\">{$item2['name']}</a> [x".$english_format_number = number_format($item['quantity'])."]</center>"; echo "<center>$".$english_format_number = number_format($item2['value'])."</center>"; echo "<center>[<a href='../item.php?sell=".$item['id']."'>Sell</a>] [<a href='../item.php?market=".$item['id']."'>Market</a>] <br>[<a href='../item.php?send=".$item['id']."'>Send</a>] [<a href='../item.php?equip=".$item['id']."'>Equip</a>]</center><br>"; echo "</td>\n"; //Close row if last column if($current_col==$max_col) { echo "<tr>\n"; $current_col = 0; //<---Changed } $current_col++; } } } //Close last row if needed if ($current_col!=1) { for(; $current_col<=$max_col; $current_col++) { echo "<td> </td>\n"; } } ?> </table> </div></div> This probably doesn't matter much, but I am using a 'Display' class for final output to the browser. The class will display the obvious HTML header and then display either the full site or the mobile site. It also displays the CSS / JS (which is previously selected in the page-specific controller code as there are variations based on server-side checks). Basically it is the ONLY class that actually needs to send anything to the browser. When I begin output and echo inside the class, that is the end of the script - there is no more server-side code to execute. If I shouldn;t echo inside the class, is it that bad to do so? Code: [Select] echo("<p class=\"commentboxContainer\"><table width=100% border=0> <tr> <td bgcolor=\"#EEE\"><strong><p class=fltlft>$name </strong>says:</p></td> </tr> </table> <table width=100% border=0 bgcolor=\"#EEE\"> <tr> <td width=40%><img src=../images/Icons/People/Anonymous.png width=64 height=64 border=1 /></td> <td width=60%>$comment',0,6</td> </tr> </table> <table width=100% border=0> <tr> <td bgcolor=\"#EEE\" ><p class=fltlft><em>$email</em></p></td> </tr> <tr> <td bgcolor=\"#EEE\" ><p class=fltlft>added on $time</p></td> </tr> </table></p><br>"); Here's my echo statement for displaying comments by users. I want to add a substr (); function to limit the amount of characters of the $comment field. That way if a user makes a long comment it doesn't push the page down. I tried putting it after the echo (); but that didn't work. I also tried putting it before $comment and that didn't even work. I know that if I make two separate echo statements, this can be done, but the field "$comment" is displayed within a table that is part of the original echo statement, so what do I do? // Other code //echo "<table border='10' cellpadding='3' cellspacing='2'>"; This works echo "<table class="my-table">"; //This does not work echo "<tr><th>First Name</th><th>Last Name</th><th>Home Phone</th><th>Cell Phone</th><th>email</th></tr>"; // keeps getting the next row until there are no more to get ================ while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table ========================== echo "<tr><td>"; echo $row['first']; echo "</td><td>"; echo $row['last']; echo "</td><td>"; echo $row['phone']; echo "</td><td>"; echo $row['cell']; echo "</td><td>"; echo $row['email']; echo "</td></tr>"; } echo "</table>"; ?> Error message: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/bayare27/public_html/content/pages/display_all_members1.php on line 29 Line 29 is: echo "<table class="my-table">"; <table class="my-table">" is a valid statement because It works in another program, but html not php I have to put a link that can be clicked around the first name and last name variables in the echo statement below. I also need to add a variable from the Select query to the link such as: 'www.phpfreaks.com/admin/customers.php?page=1&cID="VARIABLE HERE" &action=edit' I've tried a bunch of things and none seem to work. while($row = mysql_fetch_array($result)){ echo '<tr><td>' . $row['customers_firstname'] . " " . $row['customers_lastname'] . '</td><td>' . date("M. d, Y", strtotime($row['date'])) . '</td><td>' . $row['plan_id'] . '</td></tr>'; David hello, im trying to add a hyperlink that launches in a new window to the following (in the last column) any ideas? Code: [Select] echo "<tr>"; echo "<td align='center'>" . $row["ID"] . "</td>"; echo "<td align='center'>" . $row["Name"] . "</td>"; echo "<td align='center'>" . $row["jobNO"] . "</td>"; echo "<td align='center'>" . $epn . "</td>"; echo "<td align='center'>" . $cname . "</td>"; echo "<td align='center'>" . $cadd . "</td>"; //want to add a hyperlink here echo "</tr>"; } echo "</table>"; } Hi I am trying to use the nl2br function like this while ($row = mysqli_fetch_array($query)) { //May need this later to output pictures // $imageURL = 'upload/'.rawurlencode($row["filename"]); echo " <div class='divTableRow'> <div class='divTableCell'>{$row['User']} ;</div> <div class='divTableCell'>nl2br({$row['CommentText']});</div> </div> \n"; } However the output just looks like the attached picture. When I check in the sql db I can see the line breaks when doing a select * from Table ;
|