PHP - Variables To An Array And Looping
Hello,
I'm taking values for various cities/areas my members live in. One member can be placed in many cities and on registering they are required to input one city but are allowed to enter a maximum of 6 cities. So, from my posted form I have; //member ID (numeric) $MemID; $City=$_Post['City']; $City2=$_Post['City2']; $City3=$_Post['City3']; $City4=$_Post['City4']; $City5=$_Post['City5']; $City6=$_Post['City6']; Where $City must be set, but any of the other $City'x' may or may not be empty. There are two tables, 1) Cities (CityID,City) 2) Mem-Cities (MemID,CityID). I have some SQL statements which check if the city exists and if it doesn't inserts it. Then adds the $MemID to that $CityID. The way im doing this now is not very efficient like this; if(!empty($City)) { $sql="SELECT COUNT(City) AS Count FROM Cities WHERE City='$City'"; $query=mysql_query($sql); $row=mysql_fetch_array($query); if($row['Count'] == 0) { $insert="INSERT INTO Cities (City) VALUES ('$City')"; mysql_query($insert) OR die('<h1>There was an error adding into Cities</h1>' .mysql_error()); //inserting additional City } $insert="INSERT INTO Mem-Cities (MemID,CityID) SELECT '$MemID',CityID FROM Cities WHERE City='$City' LIMIT 1"; mysql_query($insert) OR die('<h1>There was an adding additional City Cities to members</h1>' .mysql_error()); //inserting additional city into Cities to Members } And repeating the code above for each city. How can I loop through each city and optimise this code? Steps 1) if $City'x' is not empty and doesn't exist in DB insert it 2) add the $MemID to that CityID Thanks! Similar TutorialsHello all. How would I loop through this array and find each "DTM" value? I want to be able to loop thru this array so that I can determine when to add 123, 456, ... 789, 1012 ... 123, 456 to a database. Hope this makes some sense... Thanks for any help on this one. Code: [Select] <?php $aryValues = array('DTM', '123', '456', 'DTM', '789', '1012', 'DTM', '123', '456'); ?> <?php $_SESSION["cart_item"] = array( 'cart_item' => array( 'id' => $id, 'product_name' => $product_name )); } $cart_items = $_SESSION["cart_item"]; foreach ($cart_items as $cart_item) { echo $cart_item["id"] . $cart_item["product_name"]; } ?>
I have tried several variations of the foreach loop like the one above and I mostly get the error message: Notice: Array to string conversion. When I use: I get the following output: array(1) { ["cart_item"]=> array(2) { ["id"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "3" } ["product_name"]=> array(2) { [0]=> string(19) "Adult Female Bike" [1]=> string(18) "Kids Unisex Bike" } } } Hi all,
I have a looping error in my PHP.
I am trying to add use a while statement to gather the users first and last name in to a string, add them to an array called $friendname, then check that the $friendname[] is only output/echoed once.
At the moment the code looks good to me but with multiple rows with the same name associated, the name is echoed the same amount of times as rows, instead of once as it is supposed to be.
Any ideas anyone?
while($row2 = mysqli_fetch_assoc($result2)) { $friendcode = $row2['Code']; $names = "SELECT FirstName, LastName from users WHERE Code = '$friendcode' AND Code != '$crewcode'"; $resultnames = mysqli_query($cxn,$names) or die ("Can't get friends names."); while($rownames = mysqli_fetch_assoc($resultnames)) { $friendname = array(); $newfriendname = ($rownames['FirstName'].' '.$rownames['LastName']); if (!in_array($newfriendname, $friendname)) { array_push($friendname, "$newfriendname"); echo $newfriendname; } } } I've got a data array that looks like this: $arraylist = ('00101001','10001010','00010100','01100101'); And another that looks like this: $arrayposition = (0,3); The idea is to loop through $arraylist and when the loop gets to the indexes/positions listed in the $arrayposition array, to change all of the 1's to 0's, then put the values back into a string variable. I have something like this at the moment: $chores = 1; $arraylist_new = ''; foreach ($arrayposition as $value) { if ($chores != '') { $oldvalnum = 0; foreach($arraylist as $string) { if ($value == $oldvalnum) { $string = str_replace("1", "0", $string); if ($arraylist_new == '') {$arraylist_new = $string;} else {$arraylist_new .= ', ' . $string;} } else { if ($arraylist_new == '') {$arraylist_new = $string;} else {$arraylist_new .= ', ' . $string;} } $oldvalnum++; } } } echo $arraylist_new; The idea is for the output to be: 00000000, 10001010, 00000000, 01100101. I know the code is hinda hectic, but could someone please help?? Thanks in advance! I have a array as follows and I want to look by key where the key is 'ABCD', 'EFGH', etc.. I am using foreach but it is not working. How can I accomplish this? Code: [Select] Array ( [0] => Array ( [ABCD] => Array ( [venue_id] => 1003 [has_dining] => X [table_count] => 0 [serves_alcohol] => X ) ) [1] => Array ( [EFGH] => Array ( [venue_id] => 1003 [has_dining] => X [table_count] => 0 [serves_alcohol] => X ) ) ) hello freaks,
I have a four-element array, which is the result of a mysql query. The array is like below. (This is of course just a snippet to show the structure; the hours of course go up to 24, then repeat; there are about ten days' worth of data.)
Array ( [dayname] => day1 [hour] => 1 [widtype] => type1 [Output] => 20 ) Array ( [dayname] => day1 [hour] => 2 [widtype] => type1 [Output] => 9 ) Array ( [dayname] => day1 [hour] => 1 [widtype] => type2 [Output] => 450 ) Array ( [dayname] => day1 [hour] => 2 [widtype] => type2 [Output] => 650 ) I want to loop through this data and output each hour's data in a separate line, like below (I included the headings just for clarity): Day Hour Type1_total Type2_total day1 1 20 450 day1 2 9 650 ... and can't seem to make this happen. Here's the code I've written: $prevday = ''; while ( $row = mysql_fetch_assoc($result)) { extract($row); $currentday = $row['dayname']; $currenthour = $row['hour']; $currentwid = $row['widtype']; while($currentday !== $prevday){ for($currenthour = 1; $currenthour <=24; $currenthour++){ if($row['widtype'] == 'type1'){ $type1_total = $row['Output'];} if($row['fuel'] == 'type2'){$type2_total = $row['Output'];} print "<pre>"; echo "$currentday, $currenthour, $type1_total, $type2_total"; print "</pre>"; } $prevday = $currentday; } }... and here's what it outputs: day1, 1, 20, , day1, 2, 20, , Clearly I have written this loop wrong but have banged my head against it for a while and wonder if it's just not possible to do what I want. Any suggestions would be hugely appreciated! Hi there, I have the following PHP code: <?php $books = array( array("title"=>"Book Title 1", "author"=>"Author 1", "publisher"=>"Publisher 1"), array("title"=>"Book Title 2", "author"=>"Author 2", "publisher"=>"Publisher 2"), array("title"=>"Book Title 3", "author"=>"Author 3", "publisher"=>"Publisher 3"), ); foreach ($books as $key => $value) { echo "$key : $value<br>"; } ?> I'm trying to loop through this code, but all it outputs is: 0: ARRAY 1: ARRAY 2: ARRAY -- It's not even pulling the proper $keys and $values. It works when there's just ONE array -- but not with this multidimensional array. Any ideas? Thanks! J Hi. I have the fokllowing xml response from an API and I am wanting to loop through the array(s) and get informaton for each of the rows below. For example I want to get the cells below for Consulting Income / 98e83040-fa3a-4185-9b9b-a49241e2bb76/150.32 then get each of the other items in the array. So the second iitem I want is Contract Income / 7d05a53d-613d-4eb2-a2fc-dcb6adb80b80 / 11748.96 and so on for each of the results returned
I am having a mental block about best way to get this information. Please advise best approach.
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Id>20f0b472-a63d-4c1f-a8de-d0fc4e860983</Id> <Status>OK</Status> <ProviderName>Xero API Previewer</ProviderName> <DateTimeUTC>2020-10-12T21:34:33.5492839Z</DateTimeUTC> <Reports> <Report> <ReportID>ProfitAndLoss</ReportID> <ReportName>Profit and Loss</ReportName> <ReportType>ProfitAndLoss</ReportType> <ReportTitles> <ReportTitle>Profit & Loss</ReportTitle> <ReportTitle>Demo Company (NZ)</ReportTitle> <ReportTitle>1 October 2020 to 31 October 2020</ReportTitle> </ReportTitles> <ReportDate>12 October 2020</ReportDate> <UpdatedDateUTC>2020-10-12T21:34:33.5492839Z</UpdatedDateUTC> <Rows> <Row> <RowType>Header</RowType> <Cells> <Cell /> <Cell> <Value>31 Oct 20</Value> </Cell> </Cells> </Row> <Row> <RowType>Section</RowType> <Title>Income</Title> <Rows> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Consulting Income</Value> <Attributes> <Attribute> <Value>98e83040-fa3a-4185-9b9b-a49241e2bb76</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>150.32</Value> <Attributes> <Attribute> <Value>98e83040-fa3a-4185-9b9b-a49241e2bb76</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Contract Income</Value> <Attributes> <Attribute> <Value>7d05a53d-613d-4eb2-a2fc-dcb6adb80b80</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>11748.96</Value> <Attributes> <Attribute> <Value>7d05a53d-613d-4eb2-a2fc-dcb6adb80b80</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Engineering Income</Value> <Attributes> <Attribute> <Value>225d8c93-251d-4a0b-9093-201acf69fe50</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>7217.29</Value> <Attributes> <Attribute> <Value>225d8c93-251d-4a0b-9093-201acf69fe50</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Equipment Income</Value> <Attributes> <Attribute> <Value>43f518a6-558f-402a-b22d-317bd64b1566</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>7377.17</Value> <Attributes> <Attribute> <Value>43f518a6-558f-402a-b22d-317bd64b1566</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Licence Income</Value> <Attributes> <Attribute> <Value>1ff30343-7bb2-4402-bb8f-a0813a7fb59e</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>3047.68</Value> <Attributes> <Attribute> <Value>1ff30343-7bb2-4402-bb8f-a0813a7fb59e</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Other Income</Value> <Attributes> <Attribute> <Value>b447935a-4b37-4f38-a841-fb3ae3b491e0</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>331.06</Value> <Attributes> <Attribute> <Value>b447935a-4b37-4f38-a841-fb3ae3b491e0</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>SummaryRow</RowType> <Cells> <Cell> <Value>Total Income</Value> </Cell> <Cell> <Value>29872.48</Value> </Cell> </Cells> </Row> </Rows> </Row> <Row> <RowType>Section</RowType> <Title>Less Cost of Sales</Title> <Rows> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Other Direct Costs</Value> <Attributes> <Attribute> <Value>9e77a829-37ea-4976-a9a8-d754e0e62f44</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>333.72</Value> <Attributes> <Attribute> <Value>9e77a829-37ea-4976-a9a8-d754e0e62f44</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>SummaryRow</RowType> <Cells> <Cell> <Value>Total Cost of Sales</Value> </Cell> <Cell> <Value>333.72</Value> </Cell> </Cells> </Row> </Rows> </Row> <Row> <RowType>Section</RowType> <Rows> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Gross Profit</Value> </Cell> <Cell> <Value>29538.76</Value> </Cell> </Cells> </Row> </Rows> </Row> <Row> <RowType>Section</RowType> <Title>Less Operating Expenses</Title> <Rows> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Accounting</Value> <Attributes> <Attribute> <Value>5e312344-4123-4cac-bb69-ad2d72d2280b</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>60.00</Value> <Attributes> <Attribute> <Value>5e312344-4123-4cac-bb69-ad2d72d2280b</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Advertising & Promotional</Value> <Attributes> <Attribute> <Value>61835d79-5a1c-4ab3-af82-af6930b38492</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>1112.21</Value> <Attributes> <Attribute> <Value>61835d79-5a1c-4ab3-af82-af6930b38492</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Bank Fees</Value> <Attributes> <Attribute> <Value>19828003-cea7-4920-b0fa-9223a2cdb0dc</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>28.75</Value> <Attributes> <Attribute> <Value>19828003-cea7-4920-b0fa-9223a2cdb0dc</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Cellphones</Value> <Attributes> <Attribute> <Value>ba533632-ac64-45c8-874e-b5a5aa6829f0</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>155.00</Value> <Attributes> <Attribute> <Value>ba533632-ac64-45c8-874e-b5a5aa6829f0</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Courier</Value> <Attributes> <Attribute> <Value>b70cadef-d8aa-46e6-b128-79e2c997fd2c</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>232.02</Value> <Attributes> <Attribute> <Value>b70cadef-d8aa-46e6-b128-79e2c997fd2c</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>EFTPOS Charges</Value> <Attributes> <Attribute> <Value>a164d99b-308e-4b2e-b3b4-a0c502214c77</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>40.00</Value> <Attributes> <Attribute> <Value>a164d99b-308e-4b2e-b3b4-a0c502214c77</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Engineering Resources</Value> <Attributes> <Attribute> <Value>53fdcc86-32ee-4fcb-b053-f449405bc7a5</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>1303.31</Value> <Attributes> <Attribute> <Value>53fdcc86-32ee-4fcb-b053-f449405bc7a5</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Entertainment - Partly Deductible</Value> <Attributes> <Attribute> <Value>d27cb87b-a3d2-4275-8948-95871fec4929</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>15.00</Value> <Attributes> <Attribute> <Value>d27cb87b-a3d2-4275-8948-95871fec4929</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Equipment Costs</Value> <Attributes> <Attribute> <Value>573a170b-6792-4cfa-b8ce-6f8fd27f5458</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>13746.93</Value> <Attributes> <Attribute> <Value>573a170b-6792-4cfa-b8ce-6f8fd27f5458</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>General Expenses</Value> <Attributes> <Attribute> <Value>11f618a8-f17e-4757-8b4a-6f24841bdb93</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>323.23</Value> <Attributes> <Attribute> <Value>11f618a8-f17e-4757-8b4a-6f24841bdb93</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Insurance</Value> <Attributes> <Attribute> <Value>92c673af-b2bc-45be-a888-a7e2c4bcc7f9</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>119.00</Value> <Attributes> <Attribute> <Value>92c673af-b2bc-45be-a888-a7e2c4bcc7f9</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>KiwiSaver Employer Contributions</Value> <Attributes> <Attribute> <Value>ab57eabe-5fa8-49d4-87dc-d5e7428323af</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>519.53</Value> <Attributes> <Attribute> <Value>ab57eabe-5fa8-49d4-87dc-d5e7428323af</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Licence Costs</Value> <Attributes> <Attribute> <Value>02dbaa55-95b9-464e-999c-c68e203cd67f</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>2176.91</Value> <Attributes> <Attribute> <Value>02dbaa55-95b9-464e-999c-c68e203cd67f</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Motor Vehicle Expenses</Value> <Attributes> <Attribute> <Value>b149780d-69bc-4f7c-a3ce-bd48f7a37de5</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>619.63</Value> <Attributes> <Attribute> <Value>b149780d-69bc-4f7c-a3ce-bd48f7a37de5</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Office Consumables & Postage</Value> <Attributes> <Attribute> <Value>b1769b51-e98e-432c-9670-d5be3054b717</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>56.00</Value> <Attributes> <Attribute> <Value>b1769b51-e98e-432c-9670-d5be3054b717</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Office Expenses</Value> <Attributes> <Attribute> <Value>935e0fbe-6749-41c8-a024-11321e44dfac</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>113.23</Value> <Attributes> <Attribute> <Value>935e0fbe-6749-41c8-a024-11321e44dfac</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Payment Processing</Value> <Attributes> <Attribute> <Value>8cd42505-ad73-46e2-932f-67952fdc4e99</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>174.51</Value> <Attributes> <Attribute> <Value>8cd42505-ad73-46e2-932f-67952fdc4e99</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Rent</Value> <Attributes> <Attribute> <Value>28113b99-6df2-4123-a2d0-5f54dcf8017b</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>1935.00</Value> <Attributes> <Attribute> <Value>28113b99-6df2-4123-a2d0-5f54dcf8017b</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Salaries & Wages</Value> <Attributes> <Attribute> <Value>be2e984a-17e1-4bf7-809c-b03a69325c90</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>17317.58</Value> <Attributes> <Attribute> <Value>be2e984a-17e1-4bf7-809c-b03a69325c90</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Security</Value> <Attributes> <Attribute> <Value>c814b08e-6558-4ac4-adce-ba76848a3a9f</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>45.00</Value> <Attributes> <Attribute> <Value>c814b08e-6558-4ac4-adce-ba76848a3a9f</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Subscriptions</Value> <Attributes> <Attribute> <Value>5ba8d64a-5b98-4bfd-a35e-7d0569e3446f</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>102.96</Value> <Attributes> <Attribute> <Value>5ba8d64a-5b98-4bfd-a35e-7d0569e3446f</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Telephone & Internet</Value> <Attributes> <Attribute> <Value>230e4cfc-f4c4-4c15-aa0d-bab02b954622</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>89.95</Value> <Attributes> <Attribute> <Value>230e4cfc-f4c4-4c15-aa0d-bab02b954622</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Travel & Accommodation - National</Value> <Attributes> <Attribute> <Value>36d33c5d-7dea-4911-9ed0-7fccc16f2b5f</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>210.43</Value> <Attributes> <Attribute> <Value>36d33c5d-7dea-4911-9ed0-7fccc16f2b5f</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Utilities</Value> <Attributes> <Attribute> <Value>2e277847-022c-48f3-8467-0207230004d6</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> <Cell> <Value>348.14</Value> <Attributes> <Attribute> <Value>2e277847-022c-48f3-8467-0207230004d6</Value> <Id>account</Id> </Attribute> </Attributes> </Cell> </Cells> </Row> <Row> <RowType>SummaryRow</RowType> <Cells> <Cell> <Value>Total Operating Expenses</Value> </Cell> <Cell> <Value>40844.32</Value> </Cell> </Cells> </Row> </Rows> </Row> <Row> <RowType>Section</RowType> <Rows> <Row> <RowType>Row</RowType> <Cells> <Cell> <Value>Net Profit</Value> </Cell> <Cell> <Value>-11305.56</Value> </Cell> </Cells> </Row> </Rows> </Row> </Rows> </Report> </Reports> </Response> Edited October 13, 2020 by Barand code tags (XML) added Hi everyone, I am very new to PHP and trying to learn so please bear with me. I have a simple form which passes fields to a PHP script: <form method="post" action="cartaction.php"> <p> <input type="submit" name="submit" value="Buy" /> <input type="hidden" name="cartaction" value="add" /> <input type="hidden" name="item" value="steelcasserole" /> </p> </form> I am trying to produce a shopping cart type page which will display the item(s) purchased. The problem is I have more than one submit button on the page for different products but with the same fields. I have tried to create the cartaction.php script to save the information from the form in a session variable which will allow me to output the item(s) purchased into this page prior to sending everything to a checkout page. I can get the information to appear for a single item but the problem is I want to output the details for each product added to the cart. At the moment, when I go back to the products page and try to add a new item to the shopping cart it just replaces the previous item that was there rather than retaining it and adding a new item underneath. I realise its probably really obvious but I am really new to this and getting myself confused! Any help on how to add an item then be able to go back to the products page (with the submit buttons for each product) and add a new item underneath the existing item in the shopping cart would be much appreciated! The code I have so far is: $submit = $_POST["submit"]; //If form is submitted, call the function and save all data into the array $_SESSION['form'] if($submit = "Buy"){setSessionVars();} function setSessionVars() { foreach($_POST as $fieldname => $fieldvalue) { $_SESSION['form'][$fieldname] = $fieldvalue; } echo "<table> <tr> <td>" .'<img src="images/'.$_SESSION['form']['item'].'.jpg"' . "<td/> <td>" . $_SESSION['form']['item'] . "</td> <td>" . '<input type="text(30)" name="value" value="1" /> <input type="submit" name="puchasednum" value="Update This One Item" />' . "</td> </tr> </table>"; }; ?> I have the session_start() function at the very top of every page. I have also attached the entire files for the two pages I talk about above. Thanks Graham I have an array which I contains multiple arrays, I would like to loop through and run a function on a certain key within the array I have the following code which works but I was wondering if there was a better method? $i = 0; foreach($Items as $Item) { $Items[$i]['key'] = custom_function($Item['key']); $i++; } The array $Items structure is as follows Code: [Select] array ( [0] => array ( [key] => 'blah' ) [1] => array ( [key] => 'blah' ) [2] => array ( [key] => 'blah' ) ) Hello all.. This one's sort of difficult to explain, but yet it should be a pretty simple solution.. What I'm trying to do is loop through an array that has either single event id's or a group of event id's, and run a condition on the change of id's while looping.. Sort of grouping each set if unique id's within a new event section.. Here's sort of a test code I put together: Code: [Select] <?php $result = array( array('event_id' => 70), array('event_id' => 70), array('event_id' => 70), array('event_id' => 95), array('event_id' => 96), array('event_id' => 97), array('event_id' => 98), array('event_id' => 99), array('event_id' => 145), array('event_id' => 145), array('event_id' => 145), array('event_id' => 145), array('event_id' => 166), array('event_id' => 166), array('event_id' => 177), array('event_id' => 200), array('event_id' => 200), array('event_id' => 200) ); $previous_id = ''; foreach($result as $row){ if ($row['event_id'] != $previous_id) { echo '<div style="background:#ccc;">New Event</div>'; } if ($previous_id > 0 && $row['event_id'] != $previous_id) { echo '<table border="1"><tr><td>'.$row['event_id'].'</td></tr></table>'; $previous_id = ''; } else { echo $row['event_id'].'<br />'; $previous_id = $row['event_id']; } } ?> It's about half way working, but there are issues when the ids change and they are either single event ids in a row, or multiple same event ids in a row.. I can't think how to set the $previous_id variable properly on each loop.. Well, figured I'd throw it out there and see what happens.. Thanks.. -First I want to say that I love this site and always check google before I submit questions but im lost. I'm into security and trying to learn a little Apache, Mysql, and PHP to understand the development cycle better. As practice im creating a notecard test review site. Create notecard Decks, add/delete questions etc. shuffle, then test yourself. I though I was doing good until I got to the page that shows the questions (test.php). Right now I pass the "deck_id" field in a GET to a "test.php?id=" page that runs a script that pulls out all the questions for that deck from my "questions" table and loops to show each question for that "deck". My loop works, (gets array from database and shows questions one by one), but it writes all the questions to the page at the same time. DUH. I want it to show one question with a "NEXT" button to show next question. This button clears the first question and writes the next one until they are all done. No grading or anything needed. Is there a way to do this gracefully? I dont know much javascript but looked up innerHTML and see i can update text that way but the PHP loop I have goes so fast it just end up showing the last question with no way to pause it between questions. Can anyone think of a way to do this? My problem is passing the data from page to page to page (I lose the array data) If this is impossible my other thoughts a 1) store current list of Q/A's in cookie (OR) 2) pass Q/A to another page with POSTS (OR) 3)store Q/A's to temporary sql table and use GETS in loop to show each one by one. "testing.php?question=1" 4) can i set a global variable that can pass the data to separate pages? 5)user iFrame so it looks like one page (dont know how) Im leaning to number 3 but want to hear your suggestions. (sorry very new) Hi all, I'm trying to set up a situation where a function loops through a multidimensional array and echos an html block for each item in the $skus['skunum'] array and also increments $i each time . ( Each is a product on the site) The catch is that I'm trying to have a function within that one that goes through the $skus['sizes'] and echoes an option if the substring (shirt size) exists. The idea being that I can manage products by adding 4 elements, Skunum, name, price and size. And well, I'm failing miserably. At this point it won't even work because of the variable scope, I'm getting Notice: Undefined variable: skus from the functions, but am not sure of the right way to get that information. Am I going about doing this entirely wrong or am I on the right track? Code: [Select] <?php $BANDNAME="Apocalypse"; $BANDCAPS="APOCALYPSE"; $BANDLOWER="apocalypse"; $SKUCAPS="FGD"; $skus = array ( "skunum"=>array ( "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112" ), "name"=>array ( "RIDDICK /TSHIRT", "MAFIA / TSHIRT", "ORACLES / TSHIRT", "AGONY / TSHIRT", "BLOODY VIOLINIST / TSHIRT", "THE VIOLATION / TSHIRT", "AGONY-TOUR DATES / TSHIRT", "BAND PHOTO / TSHIRT", "SILVER AGONY LOGO / TSHIRT", "PHOENIX-TOUR DATES / TSHIRT", "PHOENIX / TSHIRT", "BLOODY VIOLINIST / ZIP HOOD" ), "price"=>array ( "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "15.95", "42.95" ), "sizes"=>array ( "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X", "S, M, L, 1X, 2X, 3X, 4X" ) ); function makeProducts() { //Products Sizes $sizes = $skus['sizes'][$i]; $S="S";$M="M";$L="L";$XL="1";$XXL="2";$XXXL="3";$XXXXL="4"; $small = strpos($sizes,$S); $medium = strpos($sizes,$M); $large = strpos($sizes,$L); $xlarge = strpos($sizes,$XL); $xxlarge = strpos($sizes,$XXL); $xxxlarge = strpos($sizes,$XXXL); $xxxxlarge = strpos($sizes,$XXXXL); function sizeOptions () { if($small === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'S">S</OPTION>'; } if($medium === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'M">M</OPTION>'; } if($large === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'L">L</OPTION>'; } if($xlarge === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'X">XL</OPTION>'; } if($xxlarge === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'XX">XXL</OPTION>'; } if($xxxlarge === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'XXX">XXXL</OPTION>'; } if($xxxxxlarge === true) { echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'XXXX">XXXXL</OPTION>'; } } foreach ( $skus['skunum'] as $value){ echo '<!-- PRODUCT BEGIN --> <li class="product" > <a href="product_files/large/'.$SKUCAPS.$skus['skunum'][$i].'jpg" rel="lightbox"><img src="product_files/'.$SKUCAPS.$skus['skunum'][$i].'.png"></a><br> <strong>'.$skus['name'][$i].'</strong><br>('.$SKUCAPS.$skus['skunum'][$i].')<br>$'.$skus['price'][$i].'<br> <form name="'.$SKUCAPS.$skus['skunum'][$i].'" 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">' .sizeOptions(); '</SELECT> <br><br> <input type="image" src="images/addtocart.png" value="Add To Cart"> </form> </li> <!-- Product End -->'; } } // End Function ?> I'm pulling a list of topics from my DB and they are structured hierarchally using the typical id | parent_id scheme The array I end up with from the DB is one large one with keys representing the unique id of each data result...The problem is getting past the 2d scope of the array. I know this will require a recursive function and I've been at it for hours but can't seem to wrap my hurting head around it! How do I get my orignal array in this form: Array ( [0] => Array ( [1] => top parent [2] => top parent [3] => top parent [9] => top parent ) [2] => Array ( [4] => #1 child of 2 [5] => #2 child of 2 ) [3] => Array ( [6] => #1 child of 3 ) [4] => Array ( [7] => #1 child(subsub) of 4 [8] => #2 child(subsub) of 4 ) ) To look something more like this? : Array ( [1] => top parent [2] => Array ( [4] => Array ( [7] => #1 child(subsub) of 4 [8] => #2 child(subsub) of 4 ) [5] => #2 child of 2 ) [3] => Array ( [6] => #1 child of 3 ) [9] => top parent ) notice how the 2nd array has all the proper dimensions according the the DB hierarchy. Thank you ps. or maybe I should work from a different array to start from...I'm open to suggestions hello dear php-friends i currently work on a little parser project i have to find solutions for the a. fetching part b. parser part here we go - the target urls: see the overview: http://dms-schule.bildung.hessen.de/index.html http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html Search by pressing the button "type" and then choose all schools with the mouse! Results 2400 schools Here i can provide some "more help for getting the target!" - btw: see some details for this target-server: http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=9009 http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=9742 http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=9871 well - you see i have to itterate over the sites - with a function /(a loop) http://dms-schule.bildung.hessen.de/suchen/suche_schul_db.html?show_school=1000 to 10000 BTW - after fetching the page i have to see which one are empty - those ones do not need to be parsed! Well - i want to do this with curl-multi since this is the most advanced way to do this: I see i have an array that can be filled -... but i have to think about the string-concatenation - i guess that i have make some sophisticated string concatenation. this one does not fit - for($i=1;$i<=$match[1];$i++) { $url = "http://www.example.com/page?page={$i}"; and besides this i have an array - i c an fill the array. can you help me how to run in a loop with <?php /************************************\ * Multi interface in PHP with curl * * Requires PHP 5.0, Apache 2.0 and * * Curl * ************************************* * Writen By Cyborg 19671897 * * Bugfixed by Jeremy Ellman * \***********************************/ $urls = array( "http://www.google.com/", "http://www.altavista.com/", "http://www.yahoo.com/" ); $mh = curl_multi_init(); foreach ($urls as $i => $url) { $conn[$i]=curl_init($url); curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);//return data as string curl_setopt($conn[$i],CURLOPT_FOLLOWLOCATION,1);//follow redirects curl_setopt($conn[$i],CURLOPT_MAXREDIRS,2);//maximum redirects curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,10);//timeout curl_multi_add_handle ($mh,$conn[$i]); } do { $n=curl_multi_exec($mh,$active); } while ($active); foreach ($urls as $i => $url) { $res[$i]=curl_multi_getcontent($conn[$i]); curl_multi_remove_handle($mh,$conn[$i]); curl_close($conn[$i]); } curl_multi_close($mh); print_r($res); ?> This code outputs the table fields. how do I set them into an array and assign then to a variable to be inserted into a mysql database. // Load html string $dom->loadHTML($html2); // Get tables from html $tables = $dom->getElementsByTagName('table'); // Get rows from tables $rows = $tables->item(0)->getElementsByTagName('tr'); // Loop over each row foreach ($rows as $row) { // Get each column by tag name $cols = $row->getElementsByTagName('td'); // Echo values (here you can assign them in array for example) echo $cols->item(0)->nodeValue.'<br />'; echo '<hr />'; Hi folks,
Amateur coder here in need of some help.
Background: I have a dozen sensors that get recorded in a database. The sensors are not very reliable, so to smooth out the data to graph, I compare the reading to the previous reading and if they differ by a given delta I replace the reading by the previous reading (smooth out spikes). The data for each sensor is loaded into arrays that are names according to sensor numbers (ie sensor one - $sensor1, sensor 6 - $sensor6). I have go through and do the comparison on each array and just repeat the code a dozen times, but I'd like to simplify the amount of code if possible. I'm not sure how to refer to an array by using a variable.
Here is a simplified example of what I would like to do. The issue comes with - $array($i)[$x]
Hope this makes sense of what I'm tring to do. Any guidence would be greatly appreciated. Trying to become a better programmer than just repeating the same piece of code a dozen times..
<?php //define arrays of data $array1 = array (7,8,9,10,11,12); $array4 = array (13,14,15,16,17,18); $array6 = array (1,2,3,4,5,6); //define array of sensors $sensors = array(1, 4, 6); //loop through each data point of each array foreach ($sensors as $i) { for ($x = 0; $x <= 6; $x++) { echo $array($i)[$x]; } } ?> is it possible to have variables in an array? I have been trying to plot some data using phplot, a graph package but it plots different values to the ones I expect. it should be able to cope with array($variable1, $variable2) shouldn't it? All of my form POST data (from multiple forms) is managed through a file called formdata.php. Formdata.php and check_input() performs trim/stripslashes/htmlspecialchars etc on the posted variables. (it also indirectly calls relevant database functions such as insert or select). What is the correct way to add all of the variables to an array so that I can so that I can pass the array(ofvariables) to a function. ie the checked variables (only a few of them): Code: [Select] $subject = check_input($_POST['subject']); $repphone = check_input($_POST['repphone']); $repfirstname = check_input($_POST['repfirstname']); $replastname = check_input($_POST['replastname']); $streetnum = check_input($_POST['streetnum']); $streetname = check_input($_POST['streetname']); $suburb = check_input($_POST['suburb']); $postcode = check_input($_POST['postcode']); there will be many subjects and many more variables so instead of listing the variables such as: Code: [Select] function post_to_table(){ // variables global $subject;, $streetnum, $streetname, $suburb, $postcode; global $repphone, $repfirstname, $replastname; if ($subject === "specifiedsubject"){ post_to_appropriate_table($streetnum, $streetname, $suburb, $postcode, $repphone, $repfirstname, $replastname); } I would rather use an array instead of passing each variable individually: Code: [Select] function post_to_appropriate_table ($streetnum, $streetname, $suburb, $postcode $repphone, $repfirstname, $replastname) { global $database; $sql = "INSERT INTO incident ("; $sql .= "streetnum, "; $sql .= "streetname, "; $sql .= "suburb, "; $sql .= "postcode, "; $sql .= "repphone, "; $sql .= "repfirstname, "; $sql .= "replastname"; $sql .= ") "; $sql .= "VALUES ("; $sql .= "'{$streetnum}', "; $sql .= "'{$streetname}', "; $sql .= "'{$suburb}', "; $sql .= "'{$postcode}', "; $sql .= "'{$repfirstname}', "; $sql .= "'{$replastname}'"; $sql .= ") "; // echo $sql; //for debugging if required; return $database->query($sql); } how can I ditch the ever growing list of variables and use an array? Thanks. |