PHP - Compare An Array With Values
Hello Guys,
I am trying to figure this out.. I have a page that has several check boxes that when checked the values ends up as an array Like this 1, 3, 9, 6, That's what it looks like in the database. I want to compare the values (numbers) submitted with the values in my database table called benefits Here is the function I wrote this function and I want to check the values of the submitted values but I am just not sure how to compare the values, especially with a comma in the array. Code: [Select] $benefits2 = $_POST['benefits']; function check_benefits ($benefits2,$member_id,$description,$ip){ $queryb = mysql_query("SELECT * FROM benefits WHERE b_id = $benefits2"); while ($row = mysql_fetch_array($queryb)) { $benefit_list = $row['b_id']; } if (isset($benefits2)&&( $benefit_list!== $benefits2)) { Do something here! } } Thanks for your help! Similar TutorialsGuys,
How can I compare two values in same table.
servis.kalkulacija_stavke ( id BIGINT(255) NOT NULL AUTO_INCREMENT, id_kalkulacija INT(255) NOT NULL, id_cjenika INT(255) NOT NULL, vrijeme TIMESTAMP DEFAULT CURRENT_TIMESTAMP, kataloski_broj VARCHAR(15) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, kategorija_artikla VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, grupa_proizvoda VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, podgrupa_proizvoda VARCHAR(15) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, cijena_EUR DECIMAL(20, 6) NOT NULL, cijena_KN DECIMAL(20, 6) NOT NULL, carina DECIMAL(20, 6) NOT NULL, spediter DECIMAL(20, 6) NOT NULL, banka DECIMAL(20, 6) NOT NULL, transport DECIMAL(20, 6) NOT NULL, nabavna_cijena DECIMAL(20, 6) NOT NULL, drezga_marza_po_grupi DECIMAL(20, 6) NOT NULL, drezga_zarada DECIMAL(20, 6) NOT NULL, neto_VPC DECIMAL(20, 6) NOT NULL, neto_MPC DECIMAL(20, 6) NOT NULL, trosak_firme DECIMAL(20, 6) NOT NULL, trosak_firme_p DECIMAL(20, 6) NOT NULL, diler_marza_po_grupi DECIMAL(20, 6) NOT NULL, preporucena_VPC DECIMAL(20, 6) NOT NULL, preporucena_MPC DECIMAL(20, 6) NOT NULL, zarada_diler_kn DECIMAL(20, 6) NOT NULL, zarada_diler_post DECIMAL(20, 6) NOT NULL, zarada_za_nas_kn DECIMAL(20, 6) NOT NULL, zarada_za_nas_post DECIMAL(20, 6) NOT NULL, brutto_zarada_za_nas_kn DECIMAL(20, 6) NOT NULL, brutto_zarada_za_nas_post DECIMAL(20, 6) NOT NULL, godina YEAR(4) NOT NULL, PRIMARY KEY (id), INDEX id (id), INDEX kat_br (kataloski_broj), INDEX kataloski_broj (kataloski_broj, kategorija_artikla, grupa_proizvoda, podgrupa_proizvoda) ) ENGINE = MYISAM AUTO_INCREMENT = 1119990 AVG_ROW_LENGTH = 264 CHARACTER SET utf8 COLLATE utf8_general_ci;I want to compare field nabavna_cijena in last year and this year. How can I achieve that? Edited by budimir, 15 December 2014 - 08:12 AM. Hi guys! Once again i require your endless talents! I have 2 tables in my mysql DB. One table is a list of products a store offers, the other is the cart of the user. I need to find the most efficient way or retrieving the images and prices from the LIST OF PRODUCTS table based on the list of PRODUCT NUMBERS stored in the cart table of the user. List of products offered ************************* Prod # * Price * img src * ************************* 001 * 19.99* imgs/pic1.jpg * 002 * 29.99 * imgs/pic2.jpg * ************************* User's Cart ********************* Prod # * quantity * size * ********************* 001 * 10 * m * 002 * 4 * s * ******************** Basically i just want to retrieve ONLY the rows that i need (ones that the user has added to his/her cart) and ASSOCIATE the img src with the quantity and size so that i can lay it out in a table on a view_cart page... If you want a good laugh just take a look at what ive done so far... Code: [Select] $cart = mysql_query("SELECT * FROM ".$session.""); $cart_row_count = mysql_fetch_assoc($cart); $cart_row_count = mysql_num_rows($cart_row_count); if($cart_row_count != 0){ $i = 1; while($cart_result = mysql_fetch_assoc($cart)){ ${"product$i"} = $cart_result['product']; ${"size$i"} = $cart_result['size']; $i++; } $i2 = 1; $query = "product = ".${"$product$i2"}." "; while($i2 <= $i){ $i2++; $query .= "AND ".${"$product$i2"}." "; } } I have a associative array and I need to compare it to a normal array. Code: [Select] associative will have anywhere from 2-8 of these ['name'] ['color'] ['uid'] Code: [Select] normal array will have anywhere from 1-8 names ['name'] What I need to do is get the name of the people that are in the associative array that are not in the normal array. I've tried all sorts of different loops and what not and I cant seam to get anything to work in every case. So what I'm wanting to do is log changes made by users, to the database... I was thinking on the lines of first doing a query of the database row that is going to be affected, prior to it being affected, putting that row result in an array variable, then allowing the update to happen and re-querying the database row and putting the results in a separate array, to then compare to the original database row array variable... So I guess, (A): Does this make sense to do this, this way? and (B): How do you compare 2 identical arrays, then extract the value from both where the particular element does not match, e.g. has been updated? I'm going crazy. I have two arrays. The first has working hours and the array is like this: ```array(7) { [0]=> array(4) { ["morning_from"]=> string(6) "closed" ["morning_to"]=> string(6) "closed" ["afternoon_from"]=> string(5) "13:00" ["afternoon_to"]=> string(5) "19:00" }``` The second one is the busy times, the array is like this: ```array(2) { [0]=> { ["title"]=> string(13) "Test" ["start_date"]=> string(19) "2019-11-25 13:00:00" ["end_date"]=> string(19) "2019-11-25 14:00:00" } }```
Now I do a cycle of 15 times because I need to make the list for the next 15 days from today.
``` Unfortunately it does not work as I would like, it does not mark correctly if the date is occupied. I'll post a piece of code:
if($book_i == 0){
$book_day = ucwords(strftime('%d %h', strtotime($book_today_day)));
if(count($getListCalendarStartToday) > $count_list_calendar_p){
$count_more_appointment_on_some_days_morning = 0;
foreach ($getListCalendarStartToday as $key => $value) {
if($day == $book_today_day){ $book_output .= "<li>"; if(isset($user["working_time"]) && $user["working_time"]){ $book_output .= "<div><p>".$book_name_day."</p><p>".$book_day."</p></div>";
if($book_name_day == $lang["days-mon"]){
$book_morning_from = $book_array_working_time[$book_day_int]["morning_from"];
$book_morning_from_hour = (int)substr($book_morning_from, 0, 2);
$book_afternoon_from_hour = (int)substr($book_afternoon_from, 0, 2);
$book_morning_from_hour_duplicated = $book_morning_from_hour;
$book_afternoon_from_hour_duplicated = $book_afternoon_from_hour;
$book_check_closed = 0;
if($book_afternoon_from_hour == 0 && $book_afternoon_to_hour == 0){ $book_output .= "<div>";
for ($n=0; $n < $count_more_appointment_on_some_days_afternoon; $n++) {
$day_appointment_p_start_hour = date('H', strtotime($getListCalendarStartToday[$count_list_calendar_p]["start_date"]));
if($book_afternoon_from_hour_duplicated != $book_afternoon_from_hour){
if($book_afternoon_from_hour != $book_afternoon_to_hour){
$book_afternoon_from_hour_duplicated += 1; if($book_afternoon_from_min == 0){
if($day_appointment_p_start_hour == $day_appointment_p_end_hour){
$count_hour_appointment = 0;
$count_hour_appointment += 1;
$book_afternoon_from_hour_duplicated += $count_hour_appointment; break; }else{ if($book_i == 0){
if($day_appointment_p_start_min >= $book_afternoon_from_min && $day_appointment_p_start_min <= 30){ }else{
if($day_appointment_p_start_min >= $book_afternoon_from_min && $day_appointment_p_start_min >= 30){ }
$book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":00'>".number_add_leading_0($book_afternoon_from_hour).":00</a>"; }else{
$book_afternoon_from_hour_duplicated += 1;
if($book_afternoon_from_min == 0){
if($book_afternoon_from_min == $book_afternoon_to_min){
$book_output .= "</div>";
$book_output .= "</li>"; Hi all, I'm a first time poster here and I would really appreciate some guidance with my latest php challenge! I've spent the entire day googling and reading and to be honest I think I'm really over my head and need the assistance of someone experienced to advise the best way to go! I have a multi dimensional array that looks like (see below); the array is created by CodeIgniter's database library (the rows returned from a select query) but I think this is a generic PHP question as opposed to having anything to do with CI because it related to working with arrays. I'm wondering how I might go about searching the array below for the key problem_id and a value equal to a variable which I would provide. Then, when it finds an array with a the matching key and variable, it outputs the other values in that part of the array too. For example, using the sample data below. How would you recommend that I search the array for all the arrays that have the key problem_id and the value 3 and then have it output the value of the key problem_update_date and the value of the key problem_update_text. Then keep searching to find the next occurrence? Thanks in advance, as above, I've been searching really hard for the answer and believe i'm over my head! Output of print_r($updates); CI_DB_mysql_result Object ( [conn_id] => Resource id #30 [result_id] => Resource id #35 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 5 [row_data] => ) Output of print_r($updates->result_array()); Array ( [0] => Array ( [problem_update_id] => 1 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Some details about a paricular issue [problem_update_active] => 1 ) [1] => Array ( [problem_update_id] => 4 [problem_id] => 3 [problem_update_date] => 2010-10-01 [problem_update_text] => Another update about the problem with an ID of 3 [problem_update_active] => 1 ) [2] => Array ( [problem_update_id] => 5 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of four [problem_update_active] => 1 ) [3] => Array ( [problem_update_id] => 6 [problem_id] => 4 [problem_update_date] => 2010-10-12 [problem_update_text] => An update about the problem with an ID of 6 [problem_update_active] => 1 ) [4] => Array ( [problem_update_id] => 7 [problem_id] => 3 [problem_update_date] => 2010-10-12 [problem_update_text] => Some new update about the problem with the ID of 3 [problem_update_active] => 1 ) ) Another noob question here. I've got a form that I use to collect info for an estimate. I also am using Developer toolbox from Adobe. Makes my life a lot easier, but doesn't teach you anything. lol Originally I had a drop down box that would hold the value of selected item and post it into to emailTO field. Which worked just fine. Example: //This is my array. $emails_array = array( 'Landscape Maintenance'=>'me@you.com', 'Landscape Design'=>'me@me.com', 'Planting Renovation'=>'me@me.com', 'Tree Service'=>'me@me.com', 'Weed Man Lawn Fertilization'=>'me@me.com', 'Snowplowing'=>'me@me.com', 'Other'=>'me@me.com'); //This grabs the post of the drop down box and inserts the email addy. $emailObj->setTo($emails_array[$_POST['Services']]); Now I have to change the drop down box to a check box group. In which the user can select multiple services and have multiple email sent out if necessary. This is where I'm stuck. I need to get the post values compare the results to the key in $emails_array then spit out the email associated with it. Here's what I got so far. $postresult = $_POST['services']; echo 'Postresult: ' . "$postresult" . "<br />"; $myarray = explode(",", $postresult); echo 'Dirty Array: '; print_r($myarray); Here are the results Code: [Select] Postresult: Landscape Maintenance,Tree Service,Snowplowing<br> MyArray: Array ( [0] => Landscape Maintenance [1] => Tree Service [2] => Snowplowing ) How do I take either $postresult OR $myarray and compare it to $emails_array and get the unique email values for it? I want to maintain the original $postreault so i can insert it into a mysql db. Here is the code Developer toolbox produces for the checkbox group. Code: [Select] <form method="post"> <label> <input type="submit" name="Submit" id="Submit" value="Submit"> <input name="services" id="services" wdg:recordset="Recordset1" wdg:subtype="CommaCheckboxes" wdg:type="widget" wdg:displayfield="service" wdg:valuefield="service" wdg:groupby="2" style="display: none; "><span><table cellpading="0" cellspacing="0" border="0"><tbody><tr><td><label id="services_label0" htmlfor="services_commacheckbox0" for="services_commacheckbox0"><input type="checkbox" id="services_commacheckbox0" value="Landscape Maintenance" cbFor="services">Landscape Maintenance</label></td><td><label id="services_label1" htmlfor="services_commacheckbox1" for="services_commacheckbox1"><input type="checkbox" id="services_commacheckbox1" value="Landscape Design" cbFor="services">Landscape Design</label></td></tr><tr><td><label id="services_label2" htmlfor="services_commacheckbox2" for="services_commacheckbox2"><input type="checkbox" id="services_commacheckbox2" value="Planting Renovation" cbFor="services">Planting Renovation</label></td><td><label id="services_label3" htmlfor="services_commacheckbox3" for="services_commacheckbox3"><input type="checkbox" id="services_commacheckbox3" value="Tree Service" cbFor="services">Tree Service</label></td></tr><tr><td><label id="services_label4" htmlfor="services_commacheckbox4" for="services_commacheckbox4"><input type="checkbox" id="services_commacheckbox4" value="Weed Man Lawn Fertilization" cbFor="services">Weed Man Lawn Fertilization</label></td><td><label id="services_label5" htmlfor="services_commacheckbox5" for="services_commacheckbox5"><input type="checkbox" id="services_commacheckbox5" value="Snowplowing" cbFor="services">Snowplowing</label></td></tr><tr><td><label id="services_label6" htmlfor="services_commacheckbox6" for="services_commacheckbox6"><input type="checkbox" id="services_commacheckbox6" value="Other" cbFor="services">Other</label></td><td colspan="2"> </td></tr></tbody></table></span> </label> </form> I would appreciate any help on this. thank you. Hi I am trying to search an array that comes from a power shell script, the array of strings that returns is ever changing as it receives variables from the script. Therefore I need to use preg_grep to search for the word "not" I am also using another array which inverts this I then need to compare these new two arrays to the original and separate the results $working and not working into a table. I would be most grateful for any advice/solutions. The preg_grep aren't displaying anything.
<?php
//phpinfo();
ini_set('display_errors', 'On');
error_reporting(0);//E_ALL
echo '<html>
<style>
body
{
font-family:Calibri,Helvetica,sans-serif;
font-size:100%;
}
</style>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript">
function uploadJS(){
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
var textAreaValue=document.getElementById(\'ServerList\').value;
var trimmedTextAreaValue=textAreaValue.trim();
if(trimmedTextAreaValue!="") {
document.myForm.submit();
} else{
alert("Server List Text Area is Empty");
}
};
var input=document.getElementById(\'fileSelect\').value;
if(input!="") {
document.myForm.submit();
} else{
alert("You have not selected a file, please select one to proceed.");
}
var handleFileSelect = function(e) {
var files = e.target.files;
if(files.length === 1) {
document.forms.myForm.filecsv.value = files[0].name;
}
}
};
</script>
</head>';
#Upload Code
$target = "D:\Web\Upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). "
has been uploaded";
}
else {
}
if ($uploaded_size > 150000)
{ echo "Your file is too large.<br>";
$ok=0;
}
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
if (!($uploaded_type=="text/csv")) { echo "<br>";
$ok=0;
}
$target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; //This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{ echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
}
//If everything is ok we try to upload it
else {
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else
{
}
}
#Start of scripts to check Servers in textbox
if(isset($_POST['ServerList']))
{
//$arry=explode( "\r\n", $_POST['ServerList'] );
$txttrim = trim($_POST['ServerList']);
//$textAr = explode("\n", $text);
//$textAr1 = array_filter($text, 'trim'); // remove any extra \r characters left behind
$txtarea = explode("\n",$txttrim);
$txtarea = array_filter($txtarea,'trim');
foreach ($txtarea as $line => $servername)
{
//$line => $servername;
##check if querying itself....
}
echo '<pre>';
$target="D:/Web/Upload/"; $target= $target . basename( $_FILES['uploaded']['name']) ;
$csv = $target;
//$filename= $_FILES['uploaded']['name'];
$output=shell_exec("powershell -Command D:/Web/scripts/PHPfwrules.ps1 $csv < NUL");
echo '<h6>';
print_r($output);
$nomatch=preg_grep('/^not/i',$output);
echo $nomatch;
$match=preg_grep("/not/",$output,PREG_GREP_INVERT);
echo $match;
if ($working1= array_intersect($output,$match))
{
echo"<link rel=stylesheet type=text/css href=style.css>
<table class=results>
<tr>
<th>Server Name</th>
<th>Working</th>
</tr>
<tr>
<td>".$servername."</td>
<td>".$working1."</td>
</tr></table>
";
}
else if ($notworking1= array_intersect($output,$nomatch))
{
echo"<link rel=stylesheet type=text/css href=style.css>
<table class=results>
<tr>
<th>Server Name</th>
<th>Not Working</th>
</tr>
<tr>
<td>".$servername."</td>
<td>".$notworking1."</td>
</tr></table>
";
}
}
echo' <link rel=stylesheet href=dhtmlwindow.css type=text/css />
<link rel=stylesheet type=text/css href=style.css>
<script src=js/dhtmlwindow.js></script>
<hr />
<table class=results>
<tr>
<th>Server Name</th>
<th>Working</th>
<th> Not Working</th>
</tr>
<tr>
<td>'.$servername.'</td>
<td>'.$working1.'</td>
<td>'.$notworking1.'</td>
<td></td>
';
echo '</pre>';
echo '
<h3>Firewall Implementation </h3>
<h4>Please enter the server below, you can only select one server at a time. </h4>
<!--The form-->
<form action="fw2.php" method="post" name="myForm" id="myForm" enctype="multipart/form-data">
<textarea name=ServerList id=ServerList>
</textarea>
<h5>Please select a CSV file.</h5>
<input name="uploaded" type="file" /><br />
<input type="submit" value="Submit" />
<br>
</html> '
;
?>
Hello all, I have yet again trouble finding a logical solution to my problem. I'm fetching an array which can hold 1 or more values. The problem is, I want these values to ouput in my json_encode function, but this also needs to happen dynamically depending on the amount of values. I can't explain it further, so here's the code so far: Code: (php) [Select] $files = mysql_fetch_array($get_files); $a = count($files); $i = 1; while ($files) { $variablename = 'fileName' . $i; $$variablename = $files['fileName']; $i++; } $output = array( OTHER VALUES , 'fileName1' => $fileName1, 'fileName2' => $fileName2, 'fileName3' => $fileName3, ............); // How do I add the fileNames dynamically depending on how many there are? This got me thinking, I also need to dynamically GET the values with jQuery. How would I do that, when the above eventually works? Thank you. Hi there, Basic question, but I can't seem to find a page in the PHP docs that answers (though I'm sure such a one exists, I just can't seem to think up the right search terms - anyway) My question is: is there a short-hand way to use a value from an associative array as the key in another associative array? So for example, given this array: $the_array = array(0 => array('name': 'the_name', 'value' : 'the_value'), 1 => etc....); then this kind of foreach loop doesn't work Code: [Select] $new_data = array(); foreach($the_array as $element){ $new_data[$element['name']] = $element['value']; } Obviously I can just save out the values, like $name = $element['name'] --- but I was just wondering if there was a more efficient way to do it? Cheers, PS. I tried encapsulating with {} - i.e. $new_data[{$element['name']}] --- but that also fails. I have mysql select query shown below retrieving data from a database:
$sql = " SELECT student, sum( test_score ) as score FROM test INNER JOIN level ON test.level_id = level.level_id GROUP BY student" $result = $mysqli->query($sql, MYSQLI_STORE_RESULT); if($result) { while ($row = $result->fetch_array(MYSQLI_ASSOC)) { var_dump($row); } }On inspection of the retrieved array with var_dump(), i have these result below: array (size=2) 'John' => string 'English' (length=2) 'Score' => string '20' (length=3) array (size=2) 'Mary' => string 'Math' (length=3) 'Score' => string '35' (length=3) array (size=2) 'Samuel' => string 'Physics' (length=3) 'Score' => string '5' (length=3)How do I get at and print out the highest score from this set of array values within this context. I tried this echo max($row['count']) but i understandably got this feedback: Warning: max(): When only one parameter is given, it must be an array Thanks. I have an array created to control the down states of the my buttons in my navigation. Which is working well. This array will have quite a few strings in it before I'm done. I then want to conditionally load a CSS file for all the files in that array with the exception of two, is this possible? In plain in english I want to say If in array apart from these values then load my css file. My array (will have more values when complete) Code: [Select] $otherTitles = array('Product Selector', 'Avon Range', 'Knightsbridge Range', 'Kingston Range' ); My code to load a css file Code: [Select] <?php if (in_array($title, $otherTitles)) { ?> <link rel="stylesheet" type="text/css" href="homepage.css"/> <?php } ?> I want all titles in the otherTitles array to get this CSS file apart from two Product Selector and Avon Range. Thanks Richard I need to some how pull comma separated images $data[23] from this $data array and then put them into there own array so I can insert them into separate rows in a database. This is the provided array: $data[0] => VIN $data[1] => StockNumber ...(all the other number here) $data[23] => Image_URLs (comma seperated) $data[24] => Equipment // I need something like this (obviously doesn't work?!@#) $delimiter = ","; $img = explode($delimiter, $data[23]); foreach($img as $pic){ $sqlPic = "insert into class_prodimages (pid image rank) values('".$LastId['id']."', '$pic', '".rand(1,20)."')"; } Any help here? I have the following array structu Code: [Select] [0] => Array ( [id] => Array ( [$t] => http://www.google.com/mate/ ) [updated] => Array ( [$t] => 2011-08-31T11:43:05.942Z ) [category] => Array ( [0] => Array ( [scheme] => http://schemas.google.com/g/ [term] => http://schemas.google.com/contact/ ) ) [title] => Array ( [type] => text [$t] => Name ) [link] => Array ( [0] => Array ( [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo [type] => image/* [href] => https://www.google.com/mate/feeds/photos/media/ ) [1] => Array ( [rel] => self [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/contacts ) [2] => Array ( [rel] => edit [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/ ) ) [gd$email] => Array ( [0] => Array ( [rel] => http://schemas.google.com/g/2005#other [address] => email_address@gmail.com [primary] => true ) ) ) I am tried to display name and email address. I am using following method, can anyone tell me is there any better way? Thank u 4u help. NAME $name=( $emp_det[0]['title'][t]); $email =( $emp_det[0]['gdemail'][0][address]); Hello there again!
I have the following construct of array now (schematics):
array[0] array[id] = "1" array[title] = "title" array[1] array[id] = "3" array[title] = "another title"Now what i want to do, is to assign the parent-array keys to the IDs of it's contents. This is what it should look like: array[1] array[id] = "1" array[title] = "title" array[3] array[id] = "3" array[title] = "another title"How do I do that again? I know I did this some years ago, but I can not remember how. Basically I am pulling data from an XML feed on wowarmory.com Everything is working as far as getting the item names and even a list of the item levels. What I am trying to do now is add together all of the item levels in to one value that I will call $calc (I will then divide this by 16 to find the average item level). I am using a foreach loop to echo the Item Name and it's corresponding item level. I am now trying to grab the item level on each loop so I can add them all up and store them in $calc for further manipulation outside of the loop. I am a complete novice but learning, How would I go about achieving this? Of course the code I have atm is re-defining $calc every loop, so naturally it isn't working. Here is my current code: //Get the UID for each item the character has //Then look up the name for that item foreach($char_xml->characterInfo->characterTab->items->item as $item) { //get the item id $item_id = $item->attributes()->id; //get the xml doc for that item from wow armory $url = "http://www.wowarmory.com/item-info.xml?i=" . $item_id; $data = fetchXML($url); //create a SimpleXML object to parse the xml $item_xml = new SimpleXmlElement($data); //print the item's name echo "<b> Item Name: </b>" . $item_xml->itemInfo->item->attributes()->name . "</br>"; echo "<b><i> iLvL: </i></b>" . $item_xml->itemInfo->item->attributes()->level . "</br>"; //obviously this is wrong, because It is redefining $calc every loop $calc=$item_xml->itemInfo->item->attributes()->level; } //print the total item level echo $calc; How do you get the values in an array and name them as variables? Lets say I want the 0 and the 2... Array ( => 02.02.2011 [1] => 97% [2] => 538 [3] => 20 [4] => 63,247 (2.20) [5] => 77,639 [6] => 195,617 [7] => 09.13.2010 ) I am working in wordpress and things are going good. I made a form, filled it with values and have been able to store those values in a database using a wordpress function. Now my issue is that I want learn how to write them into an array and store them that way. I have been looking at several different ways but can't figure out how to code it. This is my code now Code: [Select] function add_slider_box(){ global $post; $imageurl = get_post_meta( $post->ID, 'imageurl', true ); $imagelink = get_post_meta( $post->ID, 'imagelink', true ); $imagecap = get_post_meta( $post->ID, 'imagecap', true ); ?> <div style="width: 50%" class="sliderbox"> <p><label for="imageurl">Image Url: <br /> <input type="text" class="widefat" id="imageurl" name="imageurl" value="<?php if( $imageurl ) { echo $imageurl; } ?>" /></label></p> <p><label for="imagelink">Image Link: <br /> <input type="text" class="widefat" id="imagelink" name="imagelink" value="<?php if($imagelink) { echo $imagelink; }?>" /></label></p> <p><label for="imagecap">Image Caption: <br /> <input type="text" class="widefat" id="imagecap" name="imagecap" value="<?php if( $imagecap ) { echo $imagecap; } ?>" /></label></p> <button class="remove_slide button-secondary">Remove This Slide</button> </div> <?php } function bigBusiness_save_slider_options($post_id){ global $post; if( $_POST ) { update_post_meta( $post->ID, 'imageurl', $_POST['imageurl'] ); update_post_meta( $post->ID, 'imagelink', $_POST['imagelink']); update_post_meta( $post->ID, 'imagecap', $_POST['imagecap'] ); } } so the update_post_meta function is what is saving the information into the database. How do I put that into an array? I was thinking Code: [Select] foreach($_POST as $slides) $slides[] = array( 'imageurl' => '$_POST['imageurl'], 'imagelink' => $_POST['imagelink'], 'imagecap' => $_POST['imagecap']; ); I have been able to use the data out of the database by creating a function Code: [Select] function get_half_slider_slides() { global $post; $imageurl = get_post_meta( $post->ID, 'imageurl', true ); $imagelink = get_post_meta( $post->ID, 'imagelink', true ); $imagecap = get_post_meta( $post->ID, 'imageurl', true ); return array( $imageurl, $imagelink, $imagecap ); } and then calling that function in my code like this $slide = get_half_slider_slides(); I thought since it returned an array that I would be able to do this $slides['imagelink'] $slides['imageurl'] but it doesn't work. What I am trying to do is populate the nivo-slider ( http://nivo.dev7studios.com ) with images from the database. I have been following a tutorial but since the license on the tutorial doesn't cover reusing the code, I am trying to rewrite it in a way that doesn't violate the license agreement. This code is so far is hacked together but completely different from the tut. Can you help? Thanks Hi, I have this $_POST array coming from a paypal transaction Code: [Select] Array ( [address_city] => San Jose [address_country] => United States [address_country_code] => US [address_name] => Test User [address_state] => CA [address_status] => confirmed [address_street] => 1 Main St [address_zip] => 95131 [business] => test1_biz@gmail.com [charset] => windows-1252 [custom] => [first_name] => Test [item_name1] => Product Name 1 [item_name2] => Product Name 6 [item_name3] => Product Name 8 [item_number1] => 1 [item_number2] => 6 [item_number3] => 8 [last_name] => User [mc_currency] => USD [mc_fee] => 0.82 [mc_gross] => 18.00 [mc_gross_1] => 14.00 [mc_gross_2] => 2.00 [mc_gross_3] => 2.00 [mc_handling] => 0.00 [mc_handling1] => 0.00 [mc_handling2] => 0.00 [mc_handling3] => 0.00 [mc_shipping] => 11.00 [mc_shipping1] => 11.00 [mc_shipping2] => 0.00 [mc_shipping3] => 0.00 [notify_version] => 3.4 [num_cart_items] => 3 [payer_email] => test_biz@gmail.com [payer_id] => TRCLJTHLNCJ7Q [payer_status] => verified [payment_date] => 22:52:56 Jan 27, 2012 PST [payment_fee] => 0.82 [payment_gross] => 18.00 [payment_status] => Completed [payment_type] => instant [protection_eligibility] => Eligible [quantity1] => 1 [quantity2] => 1 [quantity3] => 1 [receiver_email] => test_biz@gmail.com [receiver_id] => 74PV23B8KSK84 [residence_country] => US [tax] => 0.00 [tax1] => 0.00 [tax2] => 0.00 [tax3] => 0.00 [test_ipn] => 1 [transaction_subject] => Shopping CartProduct Name 1Product Name 6Product Name 8 [txn_id] => 7BS85664SB906284D [txn_type] => cart [verify_sign] => AJ2IuqHp8G0lIxhedAqrwRQbv8fVA4-Gum9e7DMZmEWIFrljEwFCJDfP ) 1 as you can see, there are key value pairs like this, Code: [Select] [items_name1] => Product Name 1 [items_name2] => Product Name 2 [items_name3] => Product Name 3 those keys aren't constant, I mean, it came from a shopping cart, so the item name depends on how many items were placed in the shopping cart to be purchased.. now my question is, How to loop to that post array, and get only the item_names plus their corresponding values ?, because I need them and save in a db table I'm trying to split a url into segments so that a url would be stored in an array. Example: A url like this http://localhost/application/blog/view/Test-Blog-Entry/1 Would look like this in an array: Code: [Select] array('blog', 'post', 'Test-Blog-Entry', '1'); It's an MVC url so 'blog' would be controller, 'post', would be method and the last two would be variables. I want to display the variable values of the array. Is there an easy way to do this? |