PHP - If Value Is In Array Echo Its Key
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. Similar TutorialsI 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 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 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! How can I echo the $id? Every time I post approved it refreshes my page and I can see the echoed out ID. I need to know what the ID value is. Code: [Select] if (isset($_POST['approve'])) { if(is_array($_POST['approve'])) { $keys = array_keys($_POST['approve']); $id = $keys[0]; echo $id; die(); //$sql = "UPDATE `blog_comments` SET `approved` = 1 WHERE `post_id` = '$id'"; //header("Location: " . $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] ); } } 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. Hello I Have a few problems understanding what to do with this Arrays So if i have a string that echo's : Array ( [TEST1] => Tested 1 [TEST2] => Tested 2 [TEST3] => Tested 3 ) How may i echo the middle line from the array ?? 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"/> 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']; }; ?>
I have an array ( $exifdata ) which contains the relevant bits that I require, read from exif data, as in the var_dump below. array 0 => string 'Caption' (length=7) 1 => string 'Copy' (length=4) 2 => string 'Camera' (length=6) 3 => string 'Shutter' (length=7) 4 => string 'fNo' (length=3) 5 => string 'ISO' (length=3) 6 => string 'Date' (length=4) 7 => string 'Time' (length=4) 'copy' => string 'Alf Thomas' (length=10) 'caption' => string 'Blue Sky over Auld Reekie' (length=25) 'camera' => string 'Canon PowerShot G9' (length=18) 'shutter' => string '1/800' (length=5) 'fNo' => string 'f/5.6' (length=5) 'iso' => int 200 'date' => string '07:10:2011' (length=10) 'time' => string '09:05:26' (length=8 What I am trying to do is echo each key followed by its relevant data, and thought something like this would work:- Code: [Select] foreach( $exifdata as $key => $value){ echo "$key :, $value <br />"; } The output I get from the above is as below:- 0 :, Caption 1 :, Copy 2 :, Camera 3 :, Shutter 4 :, fNo 5 :, ISO 6 :, Date 7 :, Time copy :, Foucquet caption :, Blue Sky over Auld Reekie camera :, Canon PowerShot G9 shutter :, 1/800 fNo :, f/5.6 iso :, 200 date :, 07:10:2011 time :, 09:05:26 Which more or less does what I want except that what I actually would like is this:- Copy - Foucquet Caption - Blue Sky over Auld Reekie Camera - Canon PowerShot G9 Shutter - 1/800 fNo - f/5.6 ISO - 200 Date - 07:10:2011 Time - 09:05:26 What I can't quite understand is why I am getting the two lists as an output, which simply seems to be identical to the output of the var_dump. I have peice of code which is designed enter a question into a database and the username of the person who asks the question. However, the code enters <?php echo Array; ?> into the database and not 'Tom'. I am using the same code which inserts the category of the question in the database which works. But the username comes up as <?php echo Array; ?>. Does anyone know why it shows "array"? Code: [Select] if($loggedIn) { echo "Welcome, ".$user['username'].". <a href=\"logout.php\">Logout</a>. <table width='300' border='0' align='center' cellpadding='0' cellspacing='1'> <tr> <td><form name='form1' method='post' action='phpviewquestion.php'> <table width='100%' border='0' cellspacing='1' cellpadding='3'> <tr> <td colspan='3'><strong>Your Question</strong></td> </tr> <tr> <td width='71'>Question</td> <td width='6'>:</td> <td width='600' height='50'><input name='question' type='text' id='question'></td> <td width='71'>Notes</td> <td width='6'>:</td> <td width='600' height='50'><input name='notes' type='text' id='notes'></td> </tr> <tr> <td colspan='3' align='center'><input type='image' name='image' value='Submit' src='http://www.domain.co.uk/images/submitbutton.PNG' name='image' width='100' height='53'></td> <input name='category' type='hidden' value='Furniture' id='category' > <input name='questionmaker' type='hidden' value='<?php echo $username; ?>' id='questionmaker' > </tr> </table> </form> </td> </tr> </table> </div> " ; } else { echo "Please <a href=\"login.php\">Login</a>."; } ?> Hi There, I'm feeling really dumb but i can't figure out how to get the data from an array that has been converted from xml so I can put it into a table. I've tried looping through based on simple array code but i just get array to string errors. my code looks like this $projects = array(); $xml=simplexml_load_string($response) or die("Error: Cannot create object"); foreach($xml->Jobs->Job as $item) { $projects[] = array( 'job_no' => (string)$item->ID, 'job_name' => (string)$item->Name, 'job_due' => (string)$item->DueDate, ); //array_sort_by_column($projects, 'job_due'); print_r($projects); print_r just dumps the whole lot on the page. Id like to grab each of those items and put them in a table cell then sort them by the due date etc. $job_no =""; $job_name =""; $job_due =""; //$item1 = $item->Client->Name; //$job_no = "$item->ID"; //$job_name = "$item->Name"; // $job_client = "$item1"; //$job_start = "$item->StartDate"; //$job_due = "$item->DueDate"; //$job_status = "$item->State"; $date = new DateTime($job_due); $due_date = $date->format('d-m-Y'); echo "<tr>"; echo "<td><a href='managejob.php?job_id=$job_no'><strong>$job_no</strong></td>"; echo "<td>$job_name</a></td>"; echo "<td>$job_priority</td>"; echo "<td>$job_status_1</td>"; echo "<td>$job_status_2</td>"; echo "<td>$job_status_3</td>"; echo "<td>$job_status_4</td>"; echo "<td>$job_status_5</td>"; echo "<td>$job_status_6</td>"; echo "<td>$job_status_7</td>"; echo "<td>$due_date</td>"; echo "</tr>"; } } Any help on this would be much appreciated. Thanks in advance. I'm a total noob trying to figure out how to get this function to work right. I want to echo for each Code: [Select] '<OPTION value="'.$SKUCAPS.$skunum.$size.'">'.$size.'</OPTION>' for each size in the $skus[$type][$skunum]['sizes'] array. and if no size exists echo Code: [Select] '<input value="'.$SKUCAPS.$skunum.'" type=hidden" name="item">' Code: [Select] <?php $BANDNAME="Band Name"; $BANDCAPS="BANDNAME"; $BANDLOWER="bandname"; $SKUCAPS="BND"; $SKULOWER="bnd"; $skus = array( 'shirts' => array( '165' => array( 'name' => 'World Khaos Tour <br> Limited Edition Tour Track Jacket.', 'price' => '70.00', 'sizes' => array('M', 'L', '1X' ) ), '139' => array( 'name' => 'KHAOS LEGIONS ALBUM COVER', 'price' => '15.95', 'sizes' => array('S', 'M', 'L', '1X' ) ), ), //end product type 'misc' => array( '603' => array( 'name' => 'Ring Button', 'price' => '3.00', 'sizes' => array('none') ), '602' => array( 'name' => 'Logo Button', 'price' => '3.00', 'sizes' => array('none') ) ) //end product type ); //end skus function makeProduct($type){ global $skus; foreach ( $skus[$type] as $skunum => $value) { $price = $skus[$type][$skunum]['price']; $name = $skus[$type][$skunum]['name']; echo ' <!-- PRODUCT BEGIN --> <li class="product" > <a href="images/'.$SKULOWER.$skunum.'lg.jpg" rel="lightbox"><img src="product_files/'.$SKULOWER.$skunum.'.png"></a><br> <strong>'.$name.'</strong><br>'.$SKUCAPS.$skunum.'<br>$'.$price.'<br> <form name="'.$SKUCAPS.$skunum.'" method="GET" target="_blank" action="http://www.jsrdirect.com/cgi-bin/Make-a-Store.cgi"> <input type="hidden" name="band" value="'.$BANDCAPS.'"> <input type="hidden" name="back" value="http://www.jsrdirect.com/bands/'.$BANDLOWER.'/index.html"> <strong>Qty:</strong> <input type="text" name="quantity" size="1" value="1" > <strong>Size:</strong> <SELECT name="item">'.makeSizes(). '</SELECT> <br><br> <input class="css3button" value="Add To Cart" type="image"> </form> </li> <!-- Product End -->'; } } ?> any help would be greatly appreciated Hello, im new here, and i have little experience to php and mysql as i started for 2 weeks ago. I started out with some tutorials and feeling im getting the hang of it. Enough of me, lets get to the point: <?php $con = mysql_connect('localhost',$user,$pass)or die(mysql_error()); $selectdb = mysql_select_db($selectdb)or die(mysql_error()); $sql = "SELECT * From table"; $result = mysql_query($sql); $num = mysql_num_rows($result); $myarray = array($result); $i =0; while ($i < $num){ echo $myarray[$i]; $i++; } ?> Here i have written a dummyscript that does what the original script does, it tries to fetch the keys from the table and then trying to loop it and echo out the results. The output in the browser is this: Resource id #3 I know this probably is a simple fix but i cant seem to get it sorted out. Hope some of you could help me get this baby work, or maybe have another way of doing it more "simple". Thanks in advance! Dan-Levi Hello everyone, i'm kinda stuck here although i had this working before, i don't know what's wrong here. Basically i've got a form with a drop-down menu, that menu has 3 values, x,y and z. Now that form is working with post, sending me to the next page where i have a set of arrays and a echo which should print some text based on the option chosen from the drop down menu. my code in the form is this: Code: [Select] <select name="value1" id="value1"> <option value="x">x</option> <option value="y">y</option> <option value="z">z</option> </select> the array on the next page: Code: [Select] $arrayvalue = array("x"=>"test1", "y"=>"test2", "z"=>"test3"); and the echo: Code: [Select] <?php echo $arrayvalue["value1"];?> That leaves me with an empty page. What am i doing wrong here ? am i missing something ? any help is greatly appreciated. Sabrina Hi! My question is probably simple but I've been scratching my head for hours now... I'm pretty new to php. I have an online form for orders; when submitted, an email is sent to the shop manager containing the info the client has filled in. So I pass all the info in the code below, but I can't manage to echo the array containing the order's items/qty/item_code. (I'm using Wordpress, Oxygen Builder, Metabox and Code Snippets, if that helps). Here's the code (used as a Code Snippet):
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
$name = rwmb_meta( 'name', '', $post_id );
Nom: $name
$headers = ['Content-type: text/html', "Reply-To: $email"];
Help with this would be greatly appreciated! Please let me know if any details are missing. Thanks in advance! Jordan Edited June 23 by JordanCOK, have no idea what's going on... I've done this a million times... why wont this output!?? I must have a major brain meltdown and dont know it yet!!! Code: [Select] <?php // this echoes just fine: echo $_POST['testfield']; // but this wont echo: echo if (isset($_POST['testfield'])) { $_POST['testfield'] = $test; } echo $test; /// or even this DOESNT echo either!: $_POST['testfield'] = $test; echo $test; ?> Hi All, I'm trying to echo the response from an SLA query, the query works and returns the data when I test it on an SQL application.. but when I run it on my webpage it won't echo the result. Please help? <?php $mysqli = mysqli_connect("removed", "removed", "removed", "removed"); $sql = "SELECT posts.message FROM posts INNER JOIN threads ON posts.pid=threads.firstpost WHERE threads.firstpost='1'"; $result = mysqli_query($mysqli, $sql); echo {$result['message']}; ?> Code: [Select] if (empty($_POST['selected_messages'])) message($lang_pms['Must select']); $idlist = array_values($_POST['selected_messages']); $idlist = array_map('intval', $idlist); $idlist = implode(',', array_values($idlist)); // If you're not the owner of the message, you can't delete it. $result = $db->query('SELECT DISTINCT sender_id FROM '.$db->prefix.'messages WHERE id IN ('.$idlist.') AND folder="sent" ') or error('Unable to delete the message', __FILE__, __LINE__, $db->error()); Okay, as you can see it makes intval for all the $_POST['selected_messages'] , but the problem is, when I use tamper data and just add a form field "selected_messages" it brings up a mysql error and the $idlist is blank.. so is there a way to make sure that the $_POST['selected_messages'] has to equal 'selected_messages[]' OR show error? people can just tamper data and use selected_messages without the [] and it brings up a mysql error, that's not good, I don't want users seeing my code edit: WOW Code: [Select] if (empty($idlist)){ message("No Permission"); } fixed it sorry I have a log system that allows 10 logs on each side(Left and right). I am trying to make it so that the left side has the 10 most recent logs, then the right as the next 10. Any ideas? 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! |