PHP - Echo Array With If Statement
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. Similar TutorialsWelcome 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. 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? 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> "; 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 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 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 Hi 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 want the login script to echo out a statement, and then after 5 seconds (if the user hasn't clicked the link manually) I want it to redirect with the header redirect. I'm using the sleep function for the delay of the redirect. Here's the concerned portion of the login script: // check to see if the INPUT DATA matches the DATABASE COLUMNS! if ($nickname == $dbuser_name && sha1($password) == $dbuser_password) { // set a session after login $_SESSION['user_name'] = $dbuser_name; $_SESSION['user_id'] = $dbuser_id; echo "<center>You're logged in! <a href='01.php'>Click here</a> to go to the main page.</center>"; sleep(5); // seconds to wait header ('Location: 01.php'); // password incorrect error message } else { echo "<center>Incorrect password!</center>"; } But the echo statement never gets printed out, any idea why it gets swallowed by the sleep function? I've been working on my email screen, adding a video to it, and some Css. Which seems to work quite fine. However when the page returns to the original screen (represented by the code 1.) It collapses the video image. Makes it really long horizontally and thin vertically.
If I remove this first piece, showing that the email was sent successfully. if(isset($_GET['Message'])){ echo $_GET['Message']; } It returns to the screen fine and the video looks as it should. Code1:- <?php if(isset($_GET['Message'])){ echo $_GET['Message']; } $uri = $_SERVER['REQUEST_URI']; $uri_dir = getcwd(); ?> <!DOCTYPE html> <html> <head> <link href="/LifeSaverHTML/Details/Database/stylevid2.css" rel="stylesheet" type="text/css"> <h1 class = "titler"> LifeTube </h1> <p></p> </head> <body> <video class = "tube" controls src="examplevid.mp4"> Your browser does not support the video tag. </video> <p></p> <div style="margin: 0 auto; text-align: center"> <a href='/PHPMailer/index.php?id=<?=$uri?>&dire=<?=$uri_dir?>' class="linker">Email Security</a> </div> </body> </html>
I'm using this piece of CSS = Code2. I've set all the defaults padding etc to default for testing. * { margin: 0; padding: 0; } As I say it looks great. Apart from when it returns with the email successful message. The whole thing just deflates.
Code2:- * { margin: 0; padding: 0; } body { background: url('/LifeSaverHTML/Details/Database/backround.jpg') no-repeat center center fixed; height: 100%; background-position: center; background-repeat: no-repeat; background-size: cover; } .tube { display: block; margin: auto; width: 50%; height: 20%; border: 10px solid green; } .button { margin: 0 auto; display:block; text-align: center; } .linker { background-color: #23b83c; border: 2px solid #020353; color: whitesmoke; padding: 1em 1.5em; text-align: center; text-decoration: none; font-size: 1em; display: inline-block; width: 12%; border-radius: 5px; box-shadow: 3px 3px 8px 0 #000; /* h-offset v-offset blur spread color */ } .linker:hover { color: red; } .titler { width: 20%; text-align: center; margin: 0 auto; color: #FFFFFF; border: 5px ridge rgba(124,252,0); border-radius: 36px; background: #232323; text-shadow: 0 0 5px #FFF, 0 0 10px #FFF, 0 0 15px #FFF, 0 0 20px #49ff18, 0 0 30px #49FF18, 0 0 40px #49FF18, 0 0 55px #49FF18, 0 0 75px #49ff18; } nav { width: 80%; margin: 0 auto; } nav ul { list-style: none; overflow: hidden; } nav ul li { float: left; width: 20%; } nav ul li a { text-align: center; padding: 8px 0; display: block; width: 100%; background: #cdeb8e; /* Old browsers */ background: -moz-linear-gradient(top, #cdeb8e 0%, #b0ca34 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#cdeb8e), color-stop(100%,#b0ca34)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, #cdeb8e 0%,#b0ca34 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, #cdeb8e 0%,#b0ca34 100%); /* Opera 11.10+ */ background: linear-gradient(to bottom, #cdeb8e 0%,#b0ca34 100%); /* W3C, IE10+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=’#cdeb8e’, endColorstr=’#b0ca34′,GradientType=0 ); /* IE6-9 */ } nav ul li a, nav ul li a:focus, nav ul li a:visited, nav ul li a:hover, nav ul li a:active { color: #000; text-decoration: none; } nav ul li a:hover, nav ul li a:active { background: #b0ca34; /* Old browsers */ background: -moz-linear-gradient(top, #b0ca34 0%, #96c40d 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#b0ca34), color-stop(100%,#96c40d)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, #b0ca34 0%,#96c40d 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, #b0ca34 0%,#96c40d 100%); /* Opera 11.10+ */ background: linear-gradient(to bottom, #b0ca34 0%,#96c40d 100%); /* W3C, IE10+ */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr=’#b0ca34′, endColorstr=’#96c40d’,GradientType=0 ); /* IE6-9 */ } nav ul li:first-child a { border-top-left-radius: 8px; border-bottom-left-radius: 8px; } nav ul li:last-child a { border-top-right-radius: 8px; border-bottom-right-radius: 8px; } Edited February 17, 2020 by JonnyDriller Here's the code, this is the PHP in my html contacts page (that IS in fact saved with a PHP extension): <div class = "centercontainer"> I'm trying to input the results from a query into html, but I'm just getting the variable names rather than their values. I used single quotes for the echo statement, but I think I must need to switch to double to do that. But then I have problems with the double quotes from the class names (ex. class="SideBoxTitle"). Code: [Select] <?php // get user's videos from database $conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error()); $query = "SELECT * FROM haas12_test.videos"; $result = mysqli_query($conn, $query) or die ("Couldn't execute query."); while($row = mysqli_fetch_assoc($result)) { extract($row); echo ' <div id="topBox" class="mainVideoSideBoxes" > <div class="SideBoxTitle" ><h3> $vidtitle </h3> </div><!-- close SideBoxTitle --> <div class="sideBoxVidContainer"> <div class="SideBoxScreenCast" > $vidurl </div><!-- close SideBoxScreenCast --> <div class="SideBoxDesc" ><p> $viddesc </p> </div><!-- close SideBoxDesc --> </div><!-- close sideBoxVidContainer --> </div><!-- close mainVideoSideBoxes --> </div><!-- close mainVideoSideBoxes --> '; // end echo staement } ?> I did a phpfreaks search for this, but didn't find anything specific. How can I echo out an array name? I have a function to return the array contents (for debugging purposes), and I need it to generate the actual name of the array as well. Thanks! How do I check to see if a value is in an array and then echo it? If index.php is found in the pages column. Check to see if it's value is in the $pages array. If the value matches, echo the value. Code: [Select] function fetch_feedback_top_performers() { $sql = "SELECT `page`, SUM(`like`) FROM `feedback` GROUP BY `page` ORDER BY SUM(`like`) DESC LIMIT 10"; $rs = mysql_query($sql); while($row = mysql_fetch_assoc($rs)) { $results[] = $row; } return $results; } $pages = array( 'about' => 'About', 'index' => 'Homepage', ); Echo it here... Code: [Select] <?php $results = fetch_feedback_top_performers(); foreach ($results as $result) { $page = $result['page']; $total = $result['SUM(`like`)']; $extremoved = substr($page, 0, -4); ?> <div class="mhl mvs"><span class="left"><?php echo ucwords($extremoved); ?></span><span class="f_right">(<?php echo $total; ?>)</span></div> <?php } ?> I just have no idea how to perform the check. I want to do this for security. How can I echo the array value? Here is the pre... Array ( => Array ( [count] => 2 ) ) Here is my currrent code... Code: [Select] <span class="blue"><?php echo $count; ?></span> Can I do this without using a foreach loop? Hello everyone. I'm a self learner that is very new to programming. I'm trying to print out the value of ["mid"] from a json_decode variable in the code shown below: I'm trying to use for each to access the value of "mid'. using foreach function. I know I'm not doing it the right way. please help me or show me an easy was to go around it. /////the json resopnd form the url is : {"terms":"http://www.xe.com/legal/dfs.php","privacy":"http://www.xe.com/privacy.php","from":"USD","amount":1.195,"timestamp":"2021-02-09T16:52:00Z","to":[{"quotecurrency":"NGN","mid":454.6559871014}]} ///////////////////////////////////////////////// <?php $auth = base64_encode("username:password"); $context = stream_context_create([ "http" => [ "header" => "Authorization: Basic $auth" ] ]); $homepage = file_get_contents("https://xecdapi.xe.com/v1/convert_from?to=NGN&amount=1.195", false, $context ); $json = json_decode($homepage, TRUE); foreach ($json as $price){ echo $price['mid']; }; ?>
Hello all, I've run in to a very strange problem that I've never seen before. in my "config.php" file I have something like this... <?php // the URL of the script. NO trailing slash. $xconfig['url'] = $r['cfg_site_url']; // the site name $xconfig['title'] = $r['cfg_site_title']; // the YouTube user name to populate the site with. $xconfig['youtube_user'] = $r['cfg_site_youtube']; // the video to show on the homepage $xconfig['homepage_video'] = $r['cfg_homepage_video']; ?> and in my index.php page, I have <?php include "config.php"; ?> but, also in index.php, I have this <?php echo $xconfig['title'] ; ?> but it won't echo! None of the values in $xconfig[] output anything. I've even done put this in index.php <?php var_dump($xconfig); exit; ?> and that shows the array with the contents I expected. So, why does the array have the correct contents, but will not echo? Thanks a ton! Hey guys,
I am using a function that echos out 10 images from my database. I need to have a minimum of 4 echoed images. Some categories have less than 4 images, so I would like to have a default image(s) echoed out if the database has 4 or less images.
Does anyone know how I can do this?
Below is the code I am using now to echo out the 10 images.
Thanks in advance:
<?php $query = mysql_query('SELECT image_id 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++; } ?> Default Image: <img src="http://www.mysite.com/default.jpg" width="300" height="auto"/> |