PHP - Simple In_array() Question
I have checkboxes (one for each day of the week) that I want to mark checked if the value is in an array. The value of $row['specialDaily'] could be something like 1,2,0 or 1 or 2,3,6 etc.
Code: [Select] <?php $weekdays = array($row['specialDaily']);?> <input name="monday" type="checkbox" id="monday" value="1" class="weekday" <?php if (in_array("1", $weekdays)) { echo "checked='checked'"; }?> /><input name="tuesday" type="checkbox" id="tuesday" value="2" class="weekday" <?php if (in_array("2", $weekdays)) { echo "checked='checked'"; }?> /> If the array only contains one value, the proper checkbox is checked. However, if it contains more than 1 value no checkboxes are checked. I just found the in_array() function and assumed it was exactly what I'm looking for. I assumed that since $row['specialDaily']'s value is like an array I could just plug it in. Unfortunately, (unless I'm missing something simple) I'm thinking it doesn't work the way I hoped. Thanks in advance, Twitch Similar TutorialsI can't access an array value in a multidimensional array. The if should be echoing true. $news['companyid'] is equal to 1, but I must not be using in_array correctly. Code: [Select] echo '<pre>'; print_r($own_company); if (in_array($news['companyid'], $own_company)) { echo "true"; } Output Array ( => Array ( [companyid] => 1 [companyname] => Oaysus [companytag] => [companywebsite] => http://oaysus.com [country] => 1 [state] => 0 [city] => [industry] => 4 [stage] => 2 [capitalrequested] => [guestviews] => 0 [iviews] => 3 [eviews] => 3 ) ) I have a section in one of my scripts that checks for a value in array and the value doesn't exist in the array then it add it. This works fine for single array but not for mulit-dimensional arrays. Has anyone had a similar problem and knows a good solution? Code: [Select] if(in_array($words[$i], $searchWords)){ //Do nothing }else{ $searchWords[] = $words[$i]; } I know this works and that's why I'm puzzled. I'm bringing in data from a textarea and trying to compare it. index.php Code: [Select] <form name"fruit-farm" action="fruit-check.php" method="POST"> <textarea name="fruit-list"> Apples Bananas Oranges Pickles Hamburgers Grapes </textarea> <input type"submit" name="submit" value="Check The Fruit"> </form> fruit-check.php Code: [Select] if (isset($_POST['submit'])) { //Bring in the data $fruit-list = explode("\n", $_POST['fruit-list']); //Search for Hamburgers $hamburgers = "Hamburgers"; if (in_array($hamburgers,$fruit-list)){ echo "That is not a fruit"; } } What's puzzling me is that it's not working. It must be something blantently obvious. It has something to do with the '$fruit-list' array and how it is being brought into the form. It posts fine. The reason I think this is the case is because if I simply create an array that is identical to the form coming in it works: Code: [Select] if (isset($_POST['Submit'])) { //Bring in the data $fruit-list = explode("\n", $_POST['fruit-list']); //Search for Hamburgers $hamburders = "Hamburgers"; $fruit-list = array('Apples', 'Bananas', 'Oranges', 'Pickles', 'Hamburgers', 'Grapes); if (in_array($hamburders,$fruit-list)){ echo "That is not a fruit"; } } When I 'print_r' both arrays they both look identical. What is wrong and/or different with the way I'm bringing in the textarea that's causing this not to work? Hi folks! I have two arrays: 1) print_r($users1); Array ( => 1 [1] => 2609 [2] => 2612 [3] => 2620 [4] => 2621 [5] => 2624 [6] => 2627 [7] => 2629 ) 2) print_r($users2); Array ( => 1 [1] => 2609 [2] => 2610 [3] => 2611 [4] => 2612 [5] => 2617 [6] => 2618 [7] => 2619 [8] => 2620 [9] => 2621 [10] => 2624 [11] => 2625 [12] => 2626 [13] => 2627 [14] => 2628 [15] => 2629 ) now why does nothing return when I use in_array? if(in_array($users1,$users2)) return true; all values in $users1 is in $users2... Please help, need help fast! Thank you This is PHP 101, but I'm struggling to see the obvious. I have the query below which brings back a list of quizids. I put them into an array using mysql_fetch_array. I then want to check to see if a certain quizid is actually in the quizzes table, so I thought I could simply us in_array. But it's only checking the first quizid. That doesn't make sense to me. Isn't mysql_fetch_array creating an array with all values automatically? If I actually have to use a "while" loop or something, can someone help me with how to write that code most efficiently. Code: [Select] $query = "SELECT quizid FROM quizzes"; $getquizids = mysql_query($query, $connection); if (!$getquizids) { die("Database query failed: " . mysql_error()); } else { $allquizids=mysql_fetch_array($getquizids); if (in_array($_GET['quizid'], $allquizids)) { //continue since quiz exists } else { redirect_to('index.php'); } } Hello: I'm trying to compare two mySQL result sets (for determining when to check a checkbox in an input form). $dataList['listname'] is my needle. (only the checkboxes I want to check) $data['listname'] is my haystack (the list of all checkboxes) Code: [Select] while($dataList = mysql_fetch_array($runListQuery)) { $checked = ''; echo $data['listname'].'<br /><br />'; echo $dataList['listname'].' :: ' . $data['listname'] . '::' . (string)array_search ($dataList['listname'],$data['listname'],false) .'<br>'; echo in_array($dataList['listname'],$data['listname']); echo '<br>----------------------<br>'; var_dump($data['listname']); echo '<br>----------------------<br>'; var_dump ($dataList['listname']); echo '<br>----------------------<br>'; if(in_array($dataList['listname'],$data['listname'],false)) { echo 'found!!!!!!!!!!!'; $checked = 'CHECKED'; } echo '<tr>'; echo '<td>' . $dataList['listname'] . '</td>'; echo '<td><input type="checkbox" '. $checked . ' name="'.$dataList['listname'].'" />'; echo '</tr>'; } I never hit the 'found!!!!!!!!' string. My output is below: Code: [Select] list-charter halwasiya :: list-charter:: ---------------------- string(12) "list-charter" ---------------------- string(9) "halwasiya" ---------------------- list-charter halwits :: list-charter:: ---------------------- string(12) "list-charter" ---------------------- string(7) "halwits" ---------------------- list-charter list-charter :: list-charter:: ---------------------- string(12) "list-charter" ---------------------- string(12) "list-charter" ---------------------- Code: [Select] $colorarray = isset($_POST['form']['color']) ? intval($_POST['form']['color']) : 0; $color = array( "1", "2", "3", "4", "5", "6", "7", "8", "8", "9", "10", "11", "12", "13", "14", "15", "16" ); if (in_array($color, $colorarray)) { echo "search found"; exit; }else{ echo "hacker"; exit; } i submit my $_POST['form']['color'] as 1 through 16 and it always says hacker? hi i wants to skip few categories from xml feed but have problem, here is my code which is working: foreach ($xml->channel->item as $item) { foreach ($item->children() as $child) { if ($child->getName() == 'category') { $categories[] = (string) $child; } } if((in_array("one",$categorie) || in_array("two",$categorie) in_array("three",$categorie)) { continue; } // other code here } and here is what i wants to separate categories in $skipcats but its not working. $skipcats = array("one","two","three"); foreach ($xml->channel->item as $item) { foreach ($item->children() as $child) { if ($child->getName() == 'category') { $categories[] = (string) $child; } } foreach ($skipcats as $skip) { if(in_array($skip,$categorie)) { continue; } } // other code here } please what i am missing. thanks for any guidance . I can't seem to understand why in_array is NOT finding the 'needle'. php file: Code: [Select] <?PHP $search = "WS11"; $valid_codes = file("outcode2.txt"); if(in_array($search, $valid_codes)){ echo "yes"; }else{ echo "No"; } echo $search . "<hr>"; echo "<PRE>"; print_r($valid_codes); echo "</pre>"; ?> outcode2.txt: Code: [Select] WS10 WS11 WS12 WS13 WS14 WS15 WS11 is in the outcode2.txt It is not an issue of case. Perhaps my old eyes just keep missing the problem. Any help is appreciated. I am trying to pull the username for everyone who appears within an array. How would i go about this? something like this maybe? SELECT * FROM users WHERE username (in_array($users)) Hi. I have an ID in md5 format but in the database its not. I pass the md5 ID to a page and then I am trying to load the correct record via a look up in the database of that ID. Im wondering is this code possible? <?php $query = mysql_query("SELECT * FROM table WHERE ".md5(tableID)." = $ID"); ?> Thanks Hi guys, this function below is for card validation, however anycard i enter i get the invalid card number, this is not connected to any gateways, this is just an Luhn card validation demonstration. can you tell me please why i get that? <?php if ($_POST['pay']) { $cardNo = addslashes(strip_tags($_POST['cardNo'])); function luhnCheck($cardNo) { $cardNo = str_replace(" ", "", trim($cardNo)); if(!is_numeric($cardNo) or strlen($cardNo)>19) return false; $skip = $newNum = $total = 0; for($i=0;$i<=(strlen($cardNo)-1);$i++) { if($skip ==0) { $tmpNum = ($cardNo[$i]*2); $total += (strlen($tmpNum)==2 ? (substr($tmpNum, 0, 1)+substr($tmpNum, 1)) : $tmpNum); $skip = 1; }else{ $total += $cardNo[$i]; $skip = 0; } } return (($total % 10) == 0); } /* Example Usage */ if(luhnCheck($_GET['cc'])) { echo("Valid Number."); }else{ echo("Invalid Number."); } } ?> <form name="confirm" method="post" action=""> <input type="text" name="cardNo"> <input type="submit" name="pay" value="Pay Now"> </form> I know it could be a issue with this line if(luhnCheck($_GET['cc'])) { but im wondering where should and how put this cc in my form? regards Hello everyone! I am a new user and I have a simple question. How do I set a password for a user to log in to my site with PHP? I have no MySQL database just a iOS code/host app. I know this isn’t secure but only me and my friends are on the site. Any help is appreciated. what does the # symbol do in php(e.x index.php?cat=1#privacy)? How can i use it? From what i 've seen you use it for changing a whole div without reloading the page like tabs.. I've seen this, Code: [Select] <?php } ?> in a lot of scripts at the very end of the script. What does this piece of code do? I'm teaching myself php and mysql -- and my coding is a little rough. But what i'm trying to do is display information from a mysql table in another table on a website. I can do this just fine simply displaying the table, but i want a little something different. I am only displaying one column of the table, instead of displaying it all in one column, i want to display the information in 5 columns. for example..... the table from mysql has names scott, john, james, mary, simon. with the code I have i can make it display like this: scott scott scott scott scott john john john john john james james james james james mary mary mary mary mary simon simon simon simon simon i want it to diplay like this: scott john james mary simon new names will continue down here is my code: <?php require ("connectigb.php"); $result = mysql_query ("SELECT pledgeName FROM pledgewall"); $fields_num = mysql_num_fields($result); echo "<center> <h1>TSA It Gets Better Pledge Wall</h1>"; echo "<table border='1' align='center' style='width:960px;'><tr>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) if ($cell != "") { for ($j=0; $j<5; $j++) { echo "<td>$cell</td>"; } } echo "</tr>\n"; } mysql_free_result($result); echo "</table></center>"; ?> any ideas on what i can do here, i'm sure this is a simply fix. i just haven't come across it yet. thanks for the help in advance. Lets say I have: $var=5; What would an output look like to get me: 1<br> 2<br> 3<br> 4<br> 5<br> Im at a complete loss! Thanks! if someone can help me out with this, its quite simple but I can't seem to get it to work.. Basically, this is the code I have for now, the issue is if I have 0 product or items, I don't want the sorting option to show up on the page, from the code below, what line can I add tosay that if there is no products in a category, not to show the SORT feature. thanks in advance if anyone can help. {if isset($orderby) AND isset($orderway)} <!-- Sort products --> {if isset($smarty.get.id_category) && $smarty.get.id_category} {assign var='request' value=$link->getPaginationLink('category', $category, false, true)} {elseif isset($smarty.get.id_manufacturer) && $smarty.get.id_manufacturer} {assign var='request' value=$link->getPaginationLink('manufacturer', $manufacturer, false, true)} {elseif isset($smarty.get.id_supplier) && $smarty.get.id_supplier} {assign var='request' value=$link->getPaginationLink('supplier', $supplier, false, true)} {else} {assign var='request' value=$link->getPaginationLink(false, false, false, true)} {/if} <form id="productsSortForm" action="{$request|escape:'htmlall':'UTF-8'}"> <select id="selectPrductSort" onchange="document.location.href = $(this).val();"> <option value="{$link->addSortDetails($request, $orderbydefault, $orderwaydefault)|escape:'htmlall':'UTF-8'}" {if $orderby eq $orderbydefault}selected="selected"{/if}>{l s='--'}</option> {if !$PS_CATALOG_MODE} <option value="{$link->addSortDetails($request, 'price', 'asc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'price' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='Price: lowest first'}</option> <option value="{$link->addSortDetails($request, 'price', 'desc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'price' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='Price: highest first'}</option> {/if} <option value="{$link->addSortDetails($request, 'name', 'asc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'name' AND $orderway eq 'asc'}selected="selected"{/if}>{l s='Product Name: A to Z'}</option> <option value="{$link->addSortDetails($request, 'name', 'desc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'name' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='Product Name: Z to A'}</option> {if !$PS_CATALOG_MODE} <option value="{$link->addSortDetails($request, 'quantity', 'desc')|escape:'htmlall':'UTF-8'}" {if $orderby eq 'quantity' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='In-stock first'}</option> {/if} </select> <label for="selectPrductSort">{l s='Sort by'}</label> </form> <!-- /Sort products --> {/if} hello all I'm VERY new to php and I just have a simple question how do I bold the output text of this string echo $row['name']."<BR>"; Hi, I'm working on a product page and I got it to list all the products in one column of a table, but when I add a second column it just duplicates the first column. What do I need to change? Code: [Select] <?php require_once("functions.php"); ?> <!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>Untitled Document</title> <style type="text/css"> td { border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #30C; border-right-color: #30C; border-bottom-color: #30C; border-left-color: #30C; } </style> </head> <body> <form action="" method="post" name="catalog"> <?php DatabaseConnection(); $query = "SELECT * FROM treats"; $result_set = mysql_query($query) or die(mysql_error()); $output = "<table>"; while ($row = mysql_fetch_array($result_set)) { $output .= (" <tr> <td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br /> Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td> <td width=\"400px\">" . $row['product_title']."<br /> ".$row['product_Description']."<br />" .$row['price'] . "<br /> Quantity: <input name=\"quantity\" type=\"text\" size=\"2\" /> </td> </tr> "); } $output .= "</table>"; echo $output; ?> </form> </body> </html> |