PHP - Echo Nested Arrays?
Hey guys i need to echo this: Quote
{ Up to know i tried lots of combos but can seem to get the Array correctly? Quote
$named_array3 = array( it echos : Quote
{
"error":false, "http:\\\/\\\/changeme.net:80,http:\\\/\\\/changeme.net:80,http:\\\/\\\/changeme.net:80" ]} }
but the [ needs to be at results....
Thx for the help Similar TutorialsI have a nested array. I'm trying to write code to produce the minimum value foreach first key of the $test array. So, ideally it would produce what's in the $result array. I don't know why i can't figure out how to do this. The multiple nests are throwing me off. I think i'm over thinking this. Code: [Select] <?php $test['abcd']['0'] = array('10','100','300'); $test['abcd']['1'] = array('1000','100','3000'); $test['abcd']['3'] = array('23','89','700'); $test['efgh']['0'] = array('3','55','300'); $test['efgh']['1'] = array('160','160','9000'); $test['efgh']['3'] = array('900','89','200'); //$result['abcd'] = 10; //$result['efgh'] = 3; ?> I have an assoc array thats pulled from mysql. Heres the array... Code: [Select] Array ( [0] => Array ( [ID] => 288 [User_ID] => 61 [Game_ID] => 12 [Week] => 1 [Pick] => 1 ) [1] => Array ( [ID] => 287 [User_ID] => 61 [Game_ID] => 11 [Week] => 1 [Pick] => 2 ) [2] => Array ( [ID] => 286 [User_ID] => 61 [Game_ID] => 10 [Week] => 1 [Pick] => 2 ) [3] => Array ( [ID] => 285 [User_ID] => 61 [Game_ID] => 9 [Week] => 1 [Pick] => 2 ) [4] => Array ( [ID] => 284 [User_ID] => 61 [Game_ID] => 8 [Week] => 1 [Pick] => 1 ) [5] => Array ( [ID] => 283 [User_ID] => 61 [Game_ID] => 7 [Week] => 1 [Pick] => 1 ) [6] => Array ( [ID] => 282 [User_ID] => 61 [Game_ID] => 6 [Week] => 1 [Pick] => 2 ) [7] => Array ( [ID] => 281 [User_ID] => 61 [Game_ID] => 5 [Week] => 1 [Pick] => 1 ) [8] => Array ( [ID] => 280 [User_ID] => 61 [Game_ID] => 4 [Week] => 1 [Pick] => 2 ) [9] => Array ( [ID] => 279 [User_ID] => 61 [Game_ID] => 3 [Week] => 1 [Pick] => 2 ) [10] => Array ( [ID] => 278 [User_ID] => 61 [Game_ID] => 2 [Week] => 1 [Pick] => 1 ) [11] => Array ( [ID] => 277 [User_ID] => 61 [Game_ID] => 1 [Week] => 1 [Pick] => 2 ) ) I need to search the arrays inside the main array, to look for where "Pick = 1" if so then echo Away, and some info out of that array. Or if "Pick = 2" then echo Home and some info from that array. And i need it to do foreach array inside that array. I need to search this array. Heres my code so far... $user = 61; $query = " SELECT * FROM CollegeFootballPicks WHERE CollegeFootballPicks.User_ID = $user"; $result = mysql_query($query); $affected = mysql_num_rows($result); echo $affected; echo '<br />'; for($i=0; $i<$affected; $i++){ $picks[] = mysql_fetch_assoc($result); } foreach($picks as $pic) if($pic['Pick'] == 1) echo $pic['User_ID'];echo '<br />'; echo $pic['Game_ID'];echo '<br />'; echo $pic['Week'];echo '<br />'; echo 'Away';echo '<br />';echo '<br />'; foreach($picks as $pic) if($pic['Pick'] == 2) echo $pic['User_ID'];echo '<br />'; echo $pic['Game_ID'];echo '<br />'; echo $pic['Week'];echo '<br />'; echo 'Home';echo '<br />';echo '<br />'; But this isn't working. But i have no errors. Does anyone know how i can search the nested array. <!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=iso-8859-1" /> <title>Second Testing</title> <link href="styler.css" rel="stylesheet" type="text/css" /> </head> <body> <?php error_reporting(0); $num_rows = 16; $number1 = 4; $inn = array(7,10,13,16); $outt = array(14,16,21,23); $numm = array(3,5,7,9); $r_time_in = min($inn); for($i = 0; $i <= $num_rows; $i++) { $new_r_timer_in[$i] = $r_time_in; $new_r_timer_out[$i] = $new_r_timer_in[$i] + 1; $r_time_in += 1; } // for($y = 0; $y <= $number1; $i++) // { // for($i = 0; $i <= ($num_rows - 1); $i++) // { // if(($inn[$y] <= $new_r_timer_in[$i]) || ($outt[$y] <= $new_r_timer_out[$i])) // { // $numb[$i] = $numm[$i] + $numm[$i - 1]; // } // else // { // $numb[$i] = $numm[$i - 1]; // } // } // } for($i = 0; $i <= ($num_rows - 1); $i++) { echo '<table width="526" border="0"><tr> <td width="169"> <input type="text" name="$r_in[]" value="'.$new_r_timer_in[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td> <td width="169"> <input type="text" name="$r_out[]" value="'.$new_r_timer_out[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td> <td width="169"> <input type="text" name="$r_num[]" value="'.$numb[$i].'" style="width:169px" readonly="readonly" class="text_non_color"/></td>'; } print("</tr>"); ?> </body> </html> the code above works very well in its current state. three columns are produced but only two have data, the third column is empty. when executed, the code is supposed to check whether a range(7 - is between the first element of array $inn and the first element of array $outt and then add the corresponding number in array $numm to the number in that(7 - range the commented out code is supposed to implement this idea but its not.........help. "Cannot find search daemon". I tried. I've been wracking my brain for the last two hours trying to simplify this. It seems like I'm running over the two arrays far too many times, and that I should be able to do it in one pass somehow. I have two associative arrays, the first contains a set of older options (previous version), the second is a default set of new options. I want to compare the two arrays, and strip out any old options that aren't in the new set. I want to add options from the new set that aren't already in the old, and I don't want to overwrite and of the old options that already have values. Here's a copy of the sandbox I've been working it out in; it works, but it does four separate operations on the arrays to achieve the result: http://pastebin.com/erbFujkG How do I echo the array below in a for each loop? I'm trying to output this data in a table. Array ( [u] => Array ( [0] => Array ( [row_id] => 1 [0] => Jn [1] => Bo [2] => do [3] => jilcom [4] => hocom ) [1] => Array ( [row_id] => 3 [0] => Jk [1] => Kk [2] => wd [3] => jk [4] => ) [2] => Array ( [row_id] => 5 [0] => Jn [1] => Bo [2] => on [3] => jm [4] => hm ) ) [blog] => Array ( [0] => Array ( [row_id] => 3 [0] => dk [1] => [2] => [3] => [4] => ) [1] => Array ( [row_id] => 9 [0] => 2rs [1] => dee [2] => [3] => [4] => ) ) ) OK, 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; ?> I'm having troubling with trying to create a function to spit out a single array with the following array. I can write it in a away that looks through the arrays manually. the results i am trying to generate is that each item in generated array. Array to convert array( "account" => array( "login", "register", "logout", "edit", ), "p" => array( "report", ), "array1.0" => array( "array2.0" => array( "array3.0", "array3.1 ), "array2.1", ), generating the array will look like this
Array ( [0] => account [1] => account/login [2] => account/register [3] => account/logout [4] => account/edit [5] => p [6] => p/report [7] => array1.0 [8] => array1.0/array2.0 [9] => array1.0/array2.0/array3.0 [10] => array1.0/array2.0/array3.1 [11] => array1.0/array2.1 ) The idea is that id generates a single array with combined labels and arrays inside, etc. I just can't figure out how to create a script that will create this array even If I add a new value or array.
I have this thing that i am trying to make but i cant get it to work.. can anyone help? Code: [Select] function sql_read( $dbname,$dbusername,$dbpassword ) { $names = array(); $password = array(); $connect = @mysql_connect("mysql11.000webhost.com",$dbusername,$dbpassword) or die("Could Not Connect"); @mysql_select_db ($dbname) or die("Could not find DataBase"); $query = mysql_query("select * from users"); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $names[] = $row["uname"]; $password[] = $row["password"]; $id = $row["id"]; } $return = array($names,$password,$id); }else{ $return = array(); } return $return[]; } $names = array(); $names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0]; The error i get is Code: [Select] Parse error: syntax error, unexpected '[' in /home/a5480952/public_html/sql/index.php on line 28 Line 28 is "$names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0];" Please help... i REALLLLD need help with this.. ask questions if you want to know more about what i am trying to do... thanks! 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']}; ?> I have two arrays, both with the same key values. I'd like to combine them. So for instance... Code: [Select] <?php $array1['abcd'] = array( 'value1' => "blah", 'value2' => "blahblah"); $array1['efgh'] = array( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz"); $array2['abcd'] = array('value3' => "three", 'value4' => "four"); $array2['efgh'] = array( 'value3' => "hohoho", 'value6' => "six6"); function combine_arrays($array1,$array2) { //*combining* return $single_array; } echo "<pre>"; print_r(combine_arrays($array1,$array2)); echo "</pre>"; /* would produce ['abcd'] = ( 'value1' => "blah", 'value2' => "blahblah", 'value3' => "three", 'value4' => "four" ) ['efgh'] = ( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz", 'value3' => "hohoho", 'value6' => "six6" ) */ ?> What's the easiest way to do this? 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! Hi
I try to echo out random lines of a html file and want after submit password to whole content of the same html file. I have two Problems.
1st Problem When I echo out the random lines of the html file I don't get just the text but the code of the html file as well. I don't want that. I just want the text. How to do that?
for($x = 1;$x<=40;$x++) { $lines = file("$filename.html"); echo $lines[rand(0, count($lines)-1)]."<br>"; }I tried instead of "file("$filename.html");" "readfile("$filename.html");" But then I get the random lines plus the whole content. Is there anything else I can use instead of file so that I get the random lines of text without the html code?P.S file_get_contents doesn't work either have tried that one. 2nd Problem: As you could see in my first problem I have a file called $filename.html. After I submit the value of a password I want the whole content. But it is like the program did forget what $filename.html is. How can I make the program remember what $filename.html is? Or with other words how to get the whole content of the html file? My code: if($_POST['submitPasswordIT']){ if ($_POST['passIT']== $password ){ $my_file = file_get_contents("$filename.html"); echo $my_file; } else{ echo "You entered wrong password"; } }If the password isn't correct I get: You entered wrong password. If the password is correct I get nothing. I probably need to create a path to the file "$filename.html", but I don't know exactly how to do that. im stuck on this section of code atm its failing on the line "or die(mysql_error());" i think its the $table ifs. ive tryed "" around the query but i get other errors then <?php if ($_GET['make'] == ''){ //no input //all if ($_GET['make'] == 'all'){ $table = $result6 = mysql_query("SELECT * FROM dsgi_serval where comments like '{$_GET['make']}'"); //pass if ($_GET['make'] == 'pass'){ $table = $result6 = mysql_query("SELECT * FROM dsgi_serval where comments like '{$_GET['make']}'"); //not pass if ($_GET['make'] == 'notpass'){ $table = $result6 = mysql_query("SELECT * FROM dsgi_serval where comments not like '{$_GET['make']}'"); }else{ //by make $table = $result6 = mysql_query("SELECT * FROM dsgi_serval where comments not like '{$_GET['make']}'"); } } } } ?> <div id="right"> <?php echo "$table"; or die(mysql_error()); $result=mysql_query($sql); ?> Hi guys, Was wondering if anyone can help me with this. Here's what I'm trying to do: I've retrieved values from table a from mysql and post them on a form. Code: [Select] echo "<tr><td>" .$row['id']. "</td><td>" .$row['name']. "<input type='hidden' name=name[] value=" .$row['name']. "></td><td><select name='item[]'><option value ='' selected='selected'>Please Select</option><option value ='1'>1</option><option value ='2'>2</option><option value ='3'>3</option></td></tr>"; and now i'm trying to display these information one more time on a different page. So I did the usual retrieval Code: [Select] $name = $_POST['name']; $item = $_POST['item']; Then I'm using foreach loop to retrieve the item values. Code: [Select] foreach ($item as $getitem => $value) { echo "<tr><td>"; echo $value; echo "</td></tr>"; } Here is where I'm stumped. What's the best way to associate the name to the item. while loop? Thanks for the help. i have been trying to make this work for days, this needs to also include a discount for staying over >=5 days in Tokyo, no matter what i do i always gets syntax error this class is making me crazy how do i go about getting the discount in this????? please help <?php $destination = $_POST['destination']; $numTravelers = $_POST['numTravelers']; $numNights = $_POST['numNights']; $airFare = $_POST['airFare']; $hotel = $_POST['hotel']; if ($destination == Barcelona) {$airFare = 875.00; $perNight = 85.00;} elseif ($destination == Cairo) {$airFare= 950.00; $perNight = 98.00;} elseif ($destination == Rome) {$airFare = 875.00; $perNight = 110.00;} elseif ($destination == Santiago) {$airFare = 820.00; $perNight = 85.00;} elseif ($destination == Tokyo) {$airFare = 1575.00; $perNight = 240.00;} this is where i can not figure out how to put in the discount off the airfare for staying over five days elseif ($destination == Tokyo && $numNights >=5) $tickets = $numTravelers * $airFare; $hotel = $numTravelers * $numNights * $perNight; $totalCost = $tickets + $hotel; print("<p>Destination: $destination<br />"); print("Number of people: $numTravelers<br />"); print("Number of nights: $numNights<br />"); print("Airline Tickets: $".number_format($tickets, 2)."<br />"); print("Hotel Charges: $".number_format($hotel, 2)."</p>"); print("<p><strong>TOTAL COST: $".number_format($totalCost, 2)."</strong></p>"); ?> Hello everybody, I hope someone can help me. I've got a database table with subscriber info linked to various tenders. The current layout of the table is as follows (this is dummy data): Subscriber Category Email Tender Info Dummy Company 1 Advertisting email@email.com Tender 1 Dummy Company 1 Advertisting email@email.com Tender 2 Dummy Company 1 Advertisting email@email.com Tender 3 Dummy Company 1 Advertisting email@email.com Tender 4 Dummy Company 2 Marketing email2@email.com Tender 5 Dummy Company 2 Marketing email2@email.com Tender 6 What I'm trying to do, is to send Company 1 an email with the info for all 4 tenders. If I do a normal loop, it sends 4 emails, each mail having the info only for one tender. Here's my code: Code: [Select] //Select subscriber and email address, grouped to only have one entry per sub $subs = "SELECT subscriber, email FROM tenders_temp GROUP BY subscriber"; $query = mysql_query($subs); while ($subsrs = mysql_fetch_array($query)) { //Select all tenders info $tender = "SELECT * FROM tenders_temp WHERE subscriber = '".$subsrs['subscriber']."'"; $restender = mysql_query($tender); //Send email $msg = "<html><body> <table width='100%' border='0' cellspacing='0' cellpadding='10'>"; while ($rstend = mysql_fetch_array($restender)) { $msg .= " <tr> <td valign='top'><strong>SUBSCRIBER:</strong></td> <td colspan='3'>".$rstend['subscriber']."</td> </tr> <tr> <td valign='top'><strong>CATEGORY:</strong></td> <td colspan='3'>".$rstend['tender_interests']."</td> </tr> <tr> <td width='16%' valign='top'><strong>REFERENCE: </strong></td> <td width='34%'>".$rstend['reference']."</td> <td width='15%'><strong>CLOSING:</strong></td> <td width='35%'>".$rstend['closedate']." ".$rstend['closetime']."</td> </tr> <tr> <td valign='top'><strong>DESCRIPTION:</strong></td> <td colspan='3'>".$rstend['summary']." ".$rstend['description']." ".$rstend['detail']."</td> </tr> <tr> <td valign='top'><strong>CONTACT:</strong></td> <td colspan='3'>".$rstend['organization']."; ".$rstend['contact']."; Phone: ".$rstend['dial_code']." ".$rstend['telno']."; Email: ".$rstend['email']."</td> </tr> <tr> <td valign='top'><strong>DOCUMENTATION:</strong></td> <td colspan='3'>".$rstend['documentation']."</td> </tr> <tr> <td> </td> <td> </td> <td colspan='2'> </td> </tr> <tr> <td> </td> <td> </td> <td colspan='2'> </td> </tr>"; } $msg .= " </table> </body></html>"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n"; $headers .= "From: info@tenders24.com\r\n"; $headers .= "Reply-To: info@tenders24.com\r\n"; $headers .= "Return-Path: info@tenders24.com\r\n"; $headers .= "Organization: Tenders24\r\n"; $headers .= "X-Priority: 3\r\n"; mail($subsrs['email'], "Tenders24", $msg, $headers); } Looking at this code, in theory it should work. The first query groups the entries to have one email address, then the second loop, takes each entry and loops the number of tenders inside the email. I'm getting an error with this tho. Code: [Select] Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource Can anybody please help?? Thanks in advance! Karen Hello i am new here, and new to php too. I try to learn php from about 20 days, and i am getting stuck in something, somebody asked me to make a program that will produce the following output, i have to use a nested for loop: 1 22 333 4444 55555 ........etc The code for is: Code: [Select] for($a=1;$a<10;$a++) { echo "<br>"; for ($b=0;$b<$a;$b++) echo $a; }I do know this is something elementary, but really i cannot understand how it works is there any saint that will comment the code and explain step by step whats happening there? Thank you. while($comp = mysql_fetch_assoc($company)) { echo "company name: $comp['name'] <br>"; $c = mysql_fetch_assoc(mysql_query("SELECT * FROM comp_images WHERE company='". $comp['name'] ."'")); echo $c; } When I use that, it only echos one image URL, none others. |