PHP - Only Show If Var Exists
I apologize in advance for any misuse of terminology. I find that I am rather ignorant in such things.
I'm working on a WordPress theme. Specifically on the part that shows the "next: link-to-next-post" and "previous: link-to-prev-post" links. Here is the code that I have: Code: [Select] <?php $nextvar=next_post_link('%link'); $prevvar=previous_post_link('%link'); ?> <?php if (isset($nextvar)) { ?> <h3>Next</h3> <p><?php next_post_link('%link'); ?></p> <? } ?> <?php if (isset($prevvar)) { ?> <h3>Previous</h3> <p><?php previous_post_link('%link'); ?></p> <? } ?> The first part intends to check if the links exist. And this works fine. However, the only output I'm getting are the values of $nextvar and $prevvar. The headers are nonexistent. What's wrong? Any help is much appreciated, and I thank you in advance. Similar TutorialsHey everyone, I could use some help with this one.. I need to create some php code to switch images when a certain page loads. For example: These are the pages, test1, test2,test3, so when you click on the test1 link in the navigation a certain image would show up : something like this: If test1 exists then show image1.jpg If test2 exists then show image2.jpg If test3 exists then show image2.jpg. I am doing this in wordpress and I can't figure out the if then statement.. Does anyone have any ideas?? thanks Hi community,i creating a simple website and description i analyzed my code about 3days...2hours a day. if i get the point in my head.then its say Website url is in use..but not..ok maybe i give the code below. so if you can explain whats wrong.thanks Code: [Select] <?php include_once ('connect.php'); $url= mysql_real_escape_string($_POST['url']); $desc=mysql_real_escape_string($_POST['desc']); if ($id == "krauti") { if (empty($_POST['url']) && empty($_POST['desc'])) { die('<br><center>Both Empty!.<center/><br/>'); } if (empty($_POST['url'])) { die('<br><center>URL Empty!.<center/>'); } if (empty($_POST['desc'])) { die('<br><center>desc empty fill it!.<center/>'); } $url = htmlspecialchars( stripslashes( strip_tags($_POST['url'] ) ) ); $aprasymas = htmlspecialchars( trim($_POST['desc'] ) ); $query = mysql_query("SELECT * FROM linkai WHERE url = '$url' AND 'desc' = '$desc'"); if( mysql_num_rows( $query ) == 1 ) { "INSERT INTO linkai ". "(id, url, desc) ". "VALUES('','$_POST[url]','$_POST[desc]')"; echo "Yes!."; } else if (mysql_num_rows($query) == 0 ){ echo "No!!"; } } ?> Can someone explain why this code: if (mysql_result($data,0,$SQLLargeName) = " "){ $LargeImage = "#"; } else { $LargeImage = mysql_result($data,0,$SQLLargeName); } Gives me this error? Fatal error: Can't use function return value in write context in /home/worldw44/public_html/SQL.php on line 38 And ways round it? Thanks. I want to show a cookie of a referral's username on a sign up page. The link is like this, www.mysite.com/signup?ref=johnsmith. The cookie doesn't show if I go to that url page. But it does show up once I reload the page. So I'm wondering if it's possible to show the cookie the first time around, instead of reloading the page? Here is my code. // This is in the header $url_ref_name = (!empty($_GET['ref']) ? $_GET['ref'] : null); if(!empty($url_ref_name)) { $number_of_days = 365; $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days; setcookie( "ref", $url_ref_name, $date_of_expiry,"/"); } else if(empty($url_ref_name)) { if(isset($_COOKIE['ref'])) { $user_cookie = $_COOKIE['ref']; } } else {} // This is for the sign up form if(isset($_COOKIE['ref'])) { $user_cookie = $_COOKIE['ref']; ?> <fieldset> <label>Referred By</label> <div id="ref-one"><span><?php if(!empty($user_cookie)){echo $user_cookie;} ?></span></div> <input type="hidden" name="ref" value="<?php if(!empty($user_cookie)){echo $user_cookie;} ?>" maxlength="20" placeholder="Referrer's username" readonly onfocus="this.removeAttribute('readonly');" /> </fieldset> <?php }
My error below returns (echos) that there is no data under the table sites to display. But, there is. Why is it doing this? class_l.php <?php class displaySites { function grabSites() { $query = mysql_query("SELECT title,description,votes,dates,comments,out,in FROM sites ORDER BY id DESC LIMIT 100"); if(mysql_num_rows($query) > 0) { while($fetch = mysql_fetch_assoc($query)) { echo " <div id='post-1' class='post'> <h2 class='title'><a href='#'>". $fetch['title'] ."</a></h2> <h3 class='date'>Submitted on ". $fetch['date'] ."</h3> <div class='entry'> <p>". nl2br(stripslashes($fetch['description'])) ."</p> </div> <p class='meta'><a href='#'>View</a></p> <div class='hr'> </div> </div> "; } } else { echo " <div id='post-1' class='post'> <h2 class='title'><a href='#'>Oh NOEZ!</a></h2> <h3 class='date'>". date("M-d-Y") ."</h3> <div class='entry'> <p>There are currently no sites to display!</p> </div> <p class='meta'>View</p> <div class='hr'> <hr /> </div> </div> "; } } } class register { function createAccount() { $username = mysql_real_escape_string($_POST['username']); $password = md5(sha1(sha1($_POST['password']))); $name = mysql_real_escape_string($_POST['title']); $description = mysql_real_escape_string($_POST['description']); if(isset($username) && isset($password) && isset($name) && isset($description)) { if(!$username || !$password || !$name || !$description) { echo '<table border="0"><form action="register.php" method="POST"> <tr><td>Username</td><td><input type="text" name="username" maxlength="20"></td></tr> <tr><td>Password</td><td><input type="text" name="password" maxlength="30"></td></tr> <tr><td>Site Name</td><td><input type="text" name="title" maxlength="25"></td></tr> <tr><td>Site Description</td><td><textarea cols="40" rows="18" name="description" maxlength="750"></textarea></td></tr> <tr><td>Finish!</td><td><input type="submit"></td></tr> </form></table>'; } else { $query = mysql_query("SELECT * FROM sites WHERE username = '$username' LIMIT 1"); if(mysql_num_rows($query) > 0) { echo "Sorry, an account with this username already exists."; } else { $date = date("M-d-Y"); $ip = $_SERVER['REMOTE_ADDR']; //create their account mysql_query("INSERT INTO sites VALUES (null, '$username', '$password', '$name', '$description', 0, 0, 0, '$date', '$ip', 0)"); echo "You have successfully registered your site on NovaTops! <a href='login.php'>Login</a>"; } } } else { echo "One or more of the variables are not set."; } } } class login { function verifyLogin() { $username = mysql_real_escape_string($_POST['username']); $password = md5(sha1(sha1($_POST['password']))); if(isset($username) && isset($password)) { if(!$username || !$password) { echo "<table><form action='login.php' method='POST'> <tr><td>Username</td><td><input type='text' name='username' maxlength='20'></td></tr> <tr><td>Password</td><td><input type='password' name='password' maxlength='30'></td></tr> <tr><td>Finished?</td><td><input type='submit'></td></tr> </form></table>"; } else { $query = mysql_query("SELECT * FROM sites WHERE username = '$username' AND password = '$password'LIMIT 1"); if(mysql_num_rows($query) > 0) { echo "Congratulations! You have successfully logged in! <a href='index.php'>Home</a>"; $_SESSION['user'] = $username; } else { echo "It seems you've typed in the wrong username and/or password. <a href='login.php'>Try Again</a>"; } } } else { echo "One or more of the variables aren't set."; } } } ?> index.php <?php include_once('includes/config.php'); include_once('class_l.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title><?php echo $title; ?></title> <meta name="keywords" content="" /> <meta name="description" content="" /> <link href="styles.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div class="top_text"><h1><a href="#"><?php echo $site_name; ?></a></h1> <h2><a href="#"><?php echo $slogan; ?></a></h2></div> <div id="content"> <div id="blog"> <div id="post-1" class="post"> <h2 class="title"><a href="#">Welcome to NovaTops!</a></h2> <h3 class="date">Created on April 06, 2011</h3> <div class="entry"> <p>Hello there! If your viewing this message right now, your on the awesome site called NovaTops! NovaTops allows you to register your site in our database, and then the site's script automatically starts to rank your site. The higher your rank, the more likely your site's going to get a ton of views!</p> </div> <div class="hr"> <hr /> </div> </div> <!-- end #post-1 --> <?php $displaySites = new displaySites(); $displaySites->grabSites(); ?> </div> <!-- end #blog --> <?php include_once('includes/navigation.php'); ?> <!-- end #sidebar --> <div style="clear: both; height: 1px;"></div> </div> <!-- end #content --> <div id="footer"> <p>Copyright © 2011. Design by <a href="http://www.flashtemplatesdesign.com/" title="Free Flash Templates">Free Flash Templates</a></p> <p><a href="#">Privacy Policy</a> | <a href="#">Terms of Use</a> | <a href="http://validator.w3.org/check/referer" title="This page validates as XHTML 1.0 Transitional"><abbr title="eXtensible HyperText Markup Language">XHTML</abbr></a> | <a href="http://jigsaw.w3.org/css-validator/check/referer" title="This page validates as CSS"><abbr title="Cascading Style Sheets">CSS</abbr></a></p> </div> </div> <!-- end #wrapper --> </body> </html> Hi, I have a sign up form that generates a activation key and emails it to user and they cannot login until account has been activated etc. My problem is i am not sure how to in php generate a new activation key if the generated one has already been used. I know how to do the MySQL query to check if activation key already exists but not sure how to basically tell PHP that if the activation key already exists in DB then generate a new key until one is found that does not already exist. I gather i may need a loop but not sure. Could someone please help ? Like i say i know how to do the MySQL query just need some sample code to know how to do what i asked above. Thanks for anyhelp; Much Appreciated <SCRIPT LANGUAGE="JavaScript"> <!-- Begin function popUp(URL,URL2) { day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL,'" + id + "', 'toolbar=0,scrollbars=1,location=0,top=10,left=10,statusbar=0,menubar=0,resizable=0,width=408,height=400');"); day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL2,'" + id + "', 'toolbar=0,scrollbars=1,location=0,top=10,left=450,statusbar=0,menubar=0,resizable=0,width=408,height=400');"); } // End --> </script>I have a map which shows sales people by location and if more then one sales person exists then I would need 2 pop up windows. This works great other than the fact that the areas with one sales person are getting a blank 2nd popup as a second salesperson does not exist. Any help would be appreciated. Thanks In Advance Edited by phpbeginner, 27 June 2014 - 05:52 AM. Hi all, I have the following mysql delete query that essentially deletes a row from two seperate tables "messages" and "messages2" where the id is equal to that in both of the tables. The problem I am getting (for obvious reasons) is that it will only delete from the table, if it indeed exists in both tables. Here is the query: Code: [Select] DELETE FROM messages,messages2 USING messages,messages2 WHERE messages.id = messages2.id AND messages.id = ?; How would I go about setting it so that the query always deletes from table "messages" and only deletes from "messages2" when a corresponding id exists? I think it would involve an IF EXISTS clause, but I am not sure on this, or indeed how to implement it. Thanks in advance guys Dan This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=328831.0 Maybe I am over thinking this, but I would like to see if their is another value after the current value without moving the internal array pointer. What can I do? I am displaying rows from a database onto a page using: while($row=mysql_fetch_array($query)){ echo $row['name']; } I need to figure out how to limit the rows shown to the page to 100. And if there are 100 rows on the page, a link will be displayed at the bottom, that says "Next 100". Then this will display the next 100 rows. Can you give an example how to do this please? Thanks I am making a secure registration for my website and looking for a function that will verify with a user inputted date exists. From my registration html form I am putting a month, day and year , each inputted separately into a single variable: Code: [Select] $bday = $year . "-" . $month . "-" . $day; My birthday input looks like: Code: [Select] <div class=\"Container FormBoxLeft\"> <b>Birthdate:</b> </div> <div class=\"Container FormBoxRight\"> <select name=\"month\"><option value=\"\">-Month-</option> <option value=\"01\">January</option> <option value=\"02\">February</option> <option value=\"03\">March</option> <option value=\"04\">April</option> <option value=\"05\">May</option> <option value=\"06\">June</option> <option value=\"07\">July</option> <option value=\"08\">August</option> <option value=\"09\">September</option> <option value=\"10\">October</option> <option value=\"11\">November</option> <option value=\"12\">December</option> </select> <select name=\"day\"> <option value=\"\"> -Day-</option>'; //Print 31 Days "; for ($x=1; $x<=31; $x++) { echo "<option value='".$x."'"; if(isset($_POST['submit']) && $day == $x) { echo " selected"; } echo ">".$x."</option>\n"; } echo "</select> <input name=\"year\" size=\"2\" maxlength=\"4\" type=\"text\"> So.. What I am asking is.. is there a secure way to check if the date the user inputted exists, or is there a better way to make the form more secure from that part. Upon finding the users birthdate I am also wanting to check if the user is a certain age. Hi all I am trying to write a piece of code that checks to see if an item is in the SQL database and updates the quantity if it is, or adds it as a new line if it isn't. Here's my code: Code: [Select] if (isset($_GET['add'])) { $id = $_GET['id']; $qty = $_GET['qty']; $date = date ('D M j G:i:s'); $sql_products = mysql_query("SELECT * FROM `items` WHERE `id` = '".$id."'"); $result_products = mysql_fetch_array($sql_products); $product_title = $result_products['title']; $product_price = $result_products['price']; $sql_inbasket = mysql_query("SELECT * FROM `store_basket` WHERE `id` = '".$id."' AND sessionid = '".$sessionid."'"); $isiteminbasket = mysql_num_rows($sql_inbasket); if ($isiteminbasket == 0) { mysql_query ("INSERT INTO `store_basket` SET sessionid = '".$sessionid."', productid = '".$id."', item = '".$product_title."', qty = '".$qty."', price = '".$product_price."', date = '".$date."'"); header("Location: basket.php"); } else { mysql_query("UPDATE `store_basket` SET qty = qty + 1 WHERE id = '".$id."' AND sessionid = '".$sessionid."'") ; header("Location: basket.php"); } } Every time I try and add the same item to the basket, it adds as a new line instead of updating the existing line. Many thanks for your help! Pete What is the best/proper way to check if a supplied URL goes to an actual image? My site will allow users to enter an image URL (like "http://example.com/images/monkey.jpg"). I will store the URL they enter in a MySQL database and then will call it up later and do some resizing to these images. But I don't want to try to do any resizing if the URL they supply doesn't actually go to an image, or if it's blank (i.e. the user didn't enter a URL yet). So basically, I will run a MySql query to pull back the contents of the column called "imageurl". Let's say those contents are stored in a variable called $imageurl. This is what I'm trying to accomplish.... Code: [Select] if ($theimageurl does NOT contain a URL to an image OR is blank) { // do this } else { //run my resizing code } I guess i could do this to check to see if it's blank, but not sure this is proper/most efficient way to check that part of the issue... Code: [Select] if (trim($theimageurl) == "") But greater than the "is it blank" concern is that people may enter URLs that just to a regular website so whether it's at this point in my code (or for security, I should probably do it BEFORE inserting it into my database :/ ) so curious if there are any functions that check to see if it's an image URL or not. I know of file_exists, but I'm pretty sure that's just checking to see if a file exists and doesn't check image URLs. Any info/suggestions would be appreciated as always! And if there are any recommended functions to check/clean the user-entered URL BEFORE it's entered into the database, please share Thanks! Hi all I am trying to write a piece of code that cheks to see if an item is in the SQL databse and updates the quantity if it is, or adds it as a new line if it isn't. Here's my code: Code: [Select] if (isset($_GET['add'])) { $id = $_GET['id']; $qty = $_GET['qty']; $date = date ('D M j G:i:s'); $sql_products = mysql_query("SELECT * FROM `items` WHERE `id` = '".$id."'"); $result_products = mysql_fetch_array($sql_products); $product_title = $result_products['title']; $product_price = $result_products['price']; $sql_inbasket = mysql_query("SELECT * FROM `store_basket` WHERE `id` = '".$id."' AND sessionid = '".$sessionid."'"); $isiteminbasket = mysql_num_rows($sql_inbasket); if ($isiteminbasket == 0) { mysql_query ("INSERT INTO `store_basket` SET sessionid = '".$sessionid."', productid = '".$id."', item = '".$product_title."', qty = '".$qty."', price = '".$product_price."', date = '".$date."'"); header("Location: basket.php"); } else { mysql_query("UPDATE `store_basket` SET qty = qty + 1 WHERE id = '".$id."' AND sessionid = '".$sessionid."'") ; header("Location: basket.php"); } } Every time I try and add the same item to the basket, it adds as a new line instead of updating the existing line. Many thanks for your help! Pete hello, have a question. im trying to do a select * where id exists in array $id8 here is my array Code: [Select] $result8 = mysql_query("SELECT * FROM customers WHERE whosclient = 'Lansoft'"); while($row8 = mysql_fetch_array($result8)) { $id8=$row8['id']; } here is my select code. it is only returning results for the highest number in the array Code: [Select] $result = mysql_query("SELECT * FROM timesheets WHERE status = 3 AND billable = 1 AND client = '$id8' ORDER BY date, starttime"); while($row = mysql_fetch_array($result)) { Thanks for any help! Hi folks, Working on something and I'm not sure how best to do it. It's a system whereby customers can login and see pdf files. Each customer has a unique reference number which is mirrored with a folder on the server, the folder name being the customer reference number. That's the first bit of checking. Then, within that customer folder there will be between 1 and X sub folders which are labelled by unique product reference numbers. Beyond that, each of the product reference folders will have a year folder with a folder for each month of the year going forward. Here's an example of the folder structu - Customer Number (e.g. 12345678) - Product Number (e.g. 456789) (could be more than one) - Year (e.g. 2011) (could be more than one) -Month (e.g. 01/02/03/04/05/06 etc) Essentially, I'm just unsure how to check if folders exist and display their contents if they do. Here's my code so far, which does the first part of checking to see if the product numbers exist, but not the folder checking (I've not activated the hyperlinks in this code yet). This doesn't go as far as years and months yet, just the product number: Code: [Select] $query = mysql_query("SELECT * FROM customers where id = '$id'"); while($rst = mysql_fetch_array($query)) { echo "$rst[company_name]"; } $query = mysql_query("SELECT * FROM sites where company_name = '$company'"); while($rst = mysql_fetch_array($query)) { echo "$rst[site]<br>"; if ($rst[product1] !== ""){ echo "- <a href='#'>$rst[product1]</a><br>"; } if ($rst[product1] == ""){ echo ""; } if ($rst[product2] !== ""){ echo "- <a href='#'>$rst[product2]</a><br>"; } if ($rst[product2] == ""){ echo ""; } if ($rst[product3] !== ""){ echo "- <a href='#'>$rst[product3]</a><br>"; } if ($rst[product3] == ""){ echo ""; } if ($rst[product4] !== ""){ echo "- <a href='#'>$rst[product4]</a><br>"; } if ($rst[product4] == ""){ echo ""; } if ($rst[product5] !== ""){ echo "- <a href='#'>$rst[product5]</a><br>"; } if ($rst[product5] == ""){ echo ""; } How can I check to see if a webpage exists with PHP. for example http://www.google.com/ would be true and http://fakeurl32939232.com/filepage.html = false What function/syntax would I have to use? Hello, I want to check when adding a new user, that if there are 2 records that has the same recruiter id, it's should give an error, if less, than it makes him complete the registration Also check if the National ID number is not assigned to another user, so if there is a record that has it, it should give and error, if it's unavailable, it should let him complete the registration here is my code register.html Code: [Select] <html> <form action="post.php" method="post"> <table> <tr><td>First Name: <input type="text" name="fname" /></td></tr> <tr><td>Middle Name: <input type="text" name="mname" /></td></tr> <tr><td>Last Name: <input type="text" name="lname" /></td></tr> <tr><td>Mobile: <input type="text" name="mobile" /></td></tr> <tr><td>Telephone: <input type="text" name="tel" /></td></tr> <tr><td>Address: <input type="text" name="address" /></td></tr> <tr><td>Job Title: <input type="text" name="job" /></td></tr> <tr><td>Company: <input type="text" name="company" /></td></tr> <tr><td>National ID Number: <input type="text" name="nid" /></td></tr> <tr><td>Recruiter ID: <input type="text" name="recruiteris" /></td></tr> <tr><td>Email: <input type="text" name="email" /></td></tr> <tr><td>Password: <input type="password" name="password" /></td></tr> <tr><td>Type: <input type="text" name="type" /></td></tr> <tr><td><input type="submit" /></td></tr> </table> </form></html> post.php Code: [Select] <?php $sql="INSERT INTO users (id,fname, mname, lname, mobile, tel, address, job, company, nid, recruiteris, email, password, type) VALUES ('','$_POST[fname]','$_POST[mname]','$_POST[lname]','$_POST[mobile]','$_POST[tel]','$_POST[address]','$_POST[job]','$_POST[company]','$_POST[nid]','$_POST[recruiteris]','$_POST[email]','$_POST[password]','$_POST[type]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; echo "<br />"; echo "<a href='http://waw-eg.com/admin/register.html'>Add More Client? Click Here</a><br/>"; echo "<a href='http://waw-eg.com/admin/Users.php'>View Users Click Here</a>"; mysql_close($con) ?> Code: [Select] $result = mysql_query("SELECT field_item_id_value FROM `content_type_ads`"); while($row = mysql_fetch_array($result)){ echo $row['field_item_id_value']; echo "<br />"; echo $result; } I am trying to use an "array diff()" argument and I am getting errors with it stating the above is not an array. I have used $row and $result, and neither work. Is this not dumping into an array properly? |