PHP - A Better Way Of Looping Using Foreach And Switch
Is there a nicer, cleaner more efficient and less code way of achieving this?
I have an array of rows from the database and 2 of the columns could have either of 2 values which give me 4 possibilities.
I want to loop through each of them and assign the row to one of 4 arrays so they are all separated out.
This is what i have so far. Haven't tested it yet but the theory makes sense.
if(!empty($rows)) { foreach($rows as $key => $row) { switch($row['type']) { case 'post': switch($row['archived']) { case 0: $array1[$key] = $row; break; case 1: $array2[$key] = $row; break; default: } break; case 'event': switch($row['archived']) { case 0: $array3[$key] = $row; break; case 1: $array4[$key] = $row; break; default: } break; default: } } } Similar TutorialsI'm trying to figure out what I'm doing wrong. For each recipient it runs each value which is an integer through the getUserByUserID function to verify that no values inside of the array are non users. But here's the part I'm not sure of and that's the if $recipient or $sender == FALSE. If ANY of the values are found to be non values then I want that if statement to activate. Code: [Select] foreach ($this->input->post('recipient[]') AS $recipient) { $this->users->getUserByUserID($recipient); } $sender = $this->users->getUserByUserID($this->input->post('sender')); // Verifies users were returned from database if (($recipient == FALSE) || ($sender == FALSE)) { // Users was not found in the database $outputArray['message'] = 'Either the recipient or yourself could not be found in the database!'; } $array = array("1","2","3"); foreach ($array as $value){ echo $value; } Put its only echoing out 3, not the whole array. Hi Guys I have never reall worked with xml and im stuck. I am gathering data from a class. include "example/class.php"; $data = new Class; $request = $data->List_Accounts(); $res = $data->run_xml($request); echo "<pre>"; print_r($res); echo "</pre>"; Ok so this is the output i get from the print_r(); <pre><!--?xml version="1.0" encoding="utf-8"?--> <rpc request_id="43535"> <response code="0" text="OK"> <account datatype="list"> <name datatype="username">arf34</name> <type datatype="account_type">3</type> <descr datatype="string">Linux Virtual Hosting</descr> <server datatype="int">35</server> </account> <account datatype="list"> <name datatype="username">awecerfewve</name> <type datatype="account_type">3</type> <descr datatype="string">Linux Virtual Hosting</descr> <server datatype="int">34</server> </account> </response></rpc></pre> How could i them loop through this data and pull out certain types like username and account_type using a foreach loop???? Can anyone please help with this. Thanks hi there. i have a class that queries my db, creates an array objects representing a collection of the items returned. when i pass the array back to the caller and then try to loop through the array, it keeps displaying the same record - i think it's the last record that is displayed however, when i try the same code in the class, it prints out each unique record. i tried doing a reset(myarray); on the caller but that dind't work either. any suggestions? here's the code to loop through the array: if ( count( $myarray ) > 0 ) { //reset($lineItem_array); foreach ( $myarray as $lineitem ) { echo($lineitem->getName()."~"); echo($lineItem->getOId()."~"); echo($lineItem->getProduct()."~"); echo($lineItem->getPIP()."~"); echo($lineItem->getQuantity()."~"); } } } } Im trying to assign and update table rows with a random 9 character string but am receiving the error : Fatal error: Cannot redeclare make_seed() (previously declared in ... How can I clear/reset the value before the next loop Code: [Select] else { while ($data=mysql_fetch_array($users)){ $user=$data[user]; $usersarray = array($users); foreach ($usersarray as $key => $u){ //clear pass $token = ""; srand(); //generate new password $password_length = 9; function make_seed() { list($usec, $sec) = explode(' ', microtime()); return (float) $sec + ((float) $usec * 100000); } srand(make_seed()); $alfa = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM!@#$%^&*(){}[]"; for($i = 0; $i < $password_length; $i ++) { $token .= $alfa[rand(0, strlen($alfa))]; } // insert password // setup SQL statement $SQL = " UPDATE data SET pass='$token' WHERE user='$m1' "; // execute SQL statement $makeit = mysql_db_query($userdb,"$SQL",$usercid); // check for error if (!$makeit) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } 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.. Morning, I'm working on a web site using a switch to separate different calls from a MySQL database. My style looks like this: <?php switch ($_GET['page']) { case 'news': echo ("NEWS HERE"); } break; case 'schedule'; echo ("SCHEDULE HERE"); } break; default: echo ("MAIN PAGE HERE"); break; } What I am trying to do, is specify that if the page is news, there can be an additional variable for id. I attempted before and after the break; for the news case, to make a new switch, but it isn't taking. I've looked around online, but can't find any information about this. Can anyone offer a hand? This topic has been moved to Application Frameworks. http://www.phpfreaks.com/forums/index.php?topic=355772.0 I'm not sure if the title explains it very well, but here is an image of it. I am trying to figure out how to access the option selected (equal, not equal, etc.), along with its input box, for each column (the checkboxes on the right) selected. The purpose of this is to allow a non-programming administrator user to create a custom query to access information. I can find out which checkboxes are selected no problem, but finding the accompanying drop-down option and input box is difficult for me. How do I do this? If you have a suggestion on how to change it, I'm very open. Thank you in advance! Code: [Select] if(!isset($_POST['submit'])) { echo " <form method='post'> <table> <tr> <td valign='top'>SELECT</td> <td valign='top'> <table>"; // LIST ALL COLUMNS IN A TABLE $result = mysql_query("SELECT * FROM table_name") or die(mysql_error()); $rowcount=mysql_num_rows($result); $y=mysql_num_fields($result); for ($x=0; $x<$y; $x++) { echo " <tr> <td> <input type='checkbox' name='fields[]' value='".mysql_field_name($result, $x)."'>".mysql_field_name($result, $x)." </td> </tr>"; } echo " </table> </td> <td valign='top'>FROM allocations</td> <td valign='top'>WHERE</td> <td> <table>"; // LIST ALL COLUMNS IN A TABLE $result = mysql_query("SELECT * FROM table_name") or die(mysql_error()); $rowcount=mysql_num_rows($result); $y=mysql_num_fields($result); for ($x=0; $x<$y; $x++) { echo " <tr> <td> <input type='checkbox' name='column[]' value='".mysql_field_name($result, $x)."'>".mysql_field_name($result, $x)." </td> <td> <select name='operator[]'> <option value='='>equals</option> <option value='<>'>does not equal</option> <option value='>'>greater than</option> <option value='<'>less than</option> <option value='>='>greater than or equal to</option> <option value='<='>less than or equal to</option> <option value='LIKE'>is like</option> </select> </td> <td><input type='text' name='criteria[]'></td> </tr>"; } echo " </table> </td> <td valign='top'><input type='submit' value='Process Query' name='submit' class='submit'></td> </tr> </table> </form>"; } else { echo "<b>Fields to edit:</b> "; foreach ($_POST['fields'] as $fields) { echo $fields,", "; } echo "<br><br>"; echo "<b>Columns to query:</b> "; foreach ($_POST['column'] as $columns) { echo $columns," HERE IS THE SPOT, "; } } Here is my dilemma. I'm trying to get the "class name" to return instead of the number that is associated with it. I wrote a switch so that each of the 16 cases would be associated with what the class name is. However, how do I make it output into my statement? Here is what I have so far: function getClassByNumber($x){ switch($x){ case 1: return "Alchemist"; case 2: return "Assassin"; case 3: return "Dark Arts"; case 4: return "Dark Paladin"; case 5: return "Entertainer"; case 6: return "Hunter"; case 7: return "Mage"; case 8: return "Monk"; case 9: return "Paladin"; case 10: return "Pirate"; case 11: return "Priest"; case 12: return "Psion"; case 13: return "Scholar"; case 14: return "Thief"; case 15: return "Warlock"; case 16: return "Warrior"; } } $class = getClassByNumber($username->class); echo You are now a $class. Username is defined as seeking the table I want. Class is the field where the number is stored. Thanks! Code: [Select] <?php switch($_GET['action']){ case 'delete': break; case 'edit': case 'write': if($action=='edit'){ $form_action = 'edit_ok'; echo 'Edit Page'; }else{ $form_action = 'write_ok'; echo 'Write Page'; } ?> <?php $id=$_GET['id']; $result = mysql_query("SELECT * FROM project_data WHERE id='$id'"); $row = mysql_fetch_array($result); ?> <table border = "1"> <form action="switch.php?id=<?php echo $row['ID']?>&action=[color=purple][b]<?php echo $action ?>[/b][/color]" method="post"> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" value="<?php echo $row['Date_Of_Birth']?>"/></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" <?php if($row['Gender']=='male'){echo 'checked';}?>/> Male<input type="radio" name="gender" value="female" <?php if($row['Gender']=='female'){echo 'checked';}?>/> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="">Please Select</option> <option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option> <option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option> <option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option> <option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option> <option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" value="<?php echo $row['First_Name']?>"/></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" value="<?php echo $row['Last_Name']?>" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" value="<?php echo $row['Address_Line_1']?>" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" value="<?php echo $row['Address_Line_2']?>" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="postcode" value="<?php echo $row['City']?>" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" value="<?php echo $row['Postcode']?>" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" value="<?php echo $row['Contact_No']?>" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" value="<?php echo $row['Email']?>"/></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="10" cols="30" name="note"><?php echo $row['Additional_Comment']?></textarea></td> </tr> <tr> <td><input type="submit" value="Update" /></td> <td><a href='switch.php'>Main Page</a></td> </tr> </table> </form> <?php break; //case 'write'; ?> <!--<table border = "1"> <form action="switch.php?action=write_ok" method="post"> <table> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" /></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" /> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="" selected="selcted">Please Select</option> <option value="Mr">Mr</option> <option value="Mrs">Mrs</option> <option value="Miss">Miss</option> <option value="Ms">Ms</option> <option value="Other">Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="city" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" /></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="15" cols="30" name="note">Please Enter Any Additional Information</textarea></td> </tr> <tr> <td><input type="submit" value="Submit"></td> <td><a href='switch.php'>Back</a></td> </tr> </table> </form>--> <?php //break; I have this code, which is basically a form for inputting and another for editing. As you can see I have made one part a comment as I want just the edit part to control the input and edit, which is shown at the very beginning with write case, and the seperate edit case, all I need to complete it is the part that I have highlighted in purple/bold. Does anybody have any help? Code: [Select] <!--PHP Update--> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("project1", $con); if ($_GET['action']=='delete_ok'){ }elseif ($_GET['action']=='edit_ok'){ } switch($_GET['action']){ case 'write_ok'; $sql="INSERT INTO project_data (Date_Of_Birth, Gender, Title, First_Name, Last_Name, Address_Line_1, Address_Line_2, City, Postcode, Contact_No, Email, Additional_Comment) VALUES ('".$_POST[dateofbirth]."','".$_POST[gender]."','".$_POST[title]."','".$_POST[firstname]."','".$_POST[lastname]."','".$_POST[address1]."','".$_POST[address2]."','".$_POST[city]."','".$_POST[postcode]."','".$_POST[contactno]."','".$_POST[email]."','".$_POST[note]."')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }else{ echo '<script>alert("Data Has Been Successfully Updated");</script>'; echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; } break; case 'edit_ok'; if ((!isset($_GET['id']) || trim($_GET['id']) == '')) { die('Missing record ID!'); } $id= $_GET['id']; mysql_query("UPDATE project_data SET Date_Of_Birth='".$_POST[dateofbirth]."',Gender='".$_POST[gender]."',Title='".$_POST[title]."',First_Name='".$_POST[firstname]."',Last_Name='".$_POST[surname]."',Address_Line_1='".$_POST[address1]."',Address_Line_2='".$_POST[address2]."',City='".$_POST[city]."',Postcode='".$_POST[postcode]."',Contact_No='".$_POST[contactno]."',Email='".$_POST[email]."',Additional_Comment='".$_POST[note]."' WHERE ID='".$_GET['id']. "'") or die ("Error in query: $query. " . mysql_error()); echo '<script>alert("Data Has Been Successfully Updated");</script>'; echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; } break; case 'delete_ok': #########Delete OK Start################# $id= $_GET['id']; $result = mysql_query("DELETE FROM project_data WHERE ID = '$id'") or die ("Error in query: $query. " . mysql_error()); echo '<script>alert("Data Has Been Successfully Updated");</script>'; echo '<meta http-equiv="Refresh" content="0;URL=switch.php">'; } #########Delete OK End ################# break; } ?> <!--HTML--> <?php switch($_GET['action']){ case 'delete'; break; case 'edit'; echo 'Edit page'; ?> <?php $id=$_GET['id']; $result = mysql_query("SELECT * FROM project_data WHERE id='$id'"); $row = mysql_fetch_array($result); ?> <table border = "1"> <form action="switch.php?id=<?php echo $row['ID']?> action=edit_ok" method="post"> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" value="<?php echo $row['Date_Of_Birth']?>"/></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" <?php if($row['Gender']=='male'){echo 'checked';}?>/> Male<input type="radio" name="gender" value="female" <?php if($row['Gender']=='female'){echo 'checked';}?>/> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="">Please Select</option> <option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option> <option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option> <option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option> <option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option> <option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" value="<?php echo $row['First_Name']?>"/></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" value="<?php echo $row['Last_Name']?>" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" value="<?php echo $row['Address_Line_1']?>" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" value="<?php echo $row['Address_Line_2']?>" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="postcode" value="<?php echo $row['City']?>" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" value="<?php echo $row['Postcode']?>" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" value="<?php echo $row['Contact_No']?>" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" value="<?php echo $row['Email']?>"/></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="10" cols="30" name="note"><?php echo $row['Additional_Comment']?></textarea></td> </tr> <tr> <td><input type="submit" value="Edit" /></td> <td><a href='switch.php'>Back</a></td> </tr> </table> </form> <?php break; case 'write'; ?> <form action="switch.php?action=write_ok" method="post"> <table> <tr> <td>Date Of Birth</td> <td><input type="text" name="dateofbirth" /></td> </tr> <tr> <td>Gender</td> <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" /> Female</td> </tr> <tr> <td>Title</td> <td><select name="title"> <option value="" selected="selcted">Please Select</option> <option value="Mr">Mr</option> <option value="Mrs">Mrs</option> <option value="Miss">Miss</option> <option value="Ms">Ms</option> <option value="Other">Other</option> </select></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="firstname" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname" /></td> </tr> <tr> <td>Address Line 1</td> <td><input type="text" name="address1" /></td> </tr> <tr> <td>Address Line 2</td> <td><input type="text" name="address2" /></td> </tr> <tr> <td>City</td> <td><input type="text" name="city" /></td> </tr> <tr> <td>Postcode</td> <td><input type="text" name="postcode" /></td> </tr> <tr> <td>Contact No</td> <td><input type="text" name="contactno" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" /></td> </tr> <tr> <td>Additional Info</td> <td><textarea rows="15" cols="30" name="note">Please Enter Any Additional Information</textarea></td> </tr> <tr> <td><input type="submit" value="Submit"></td> <td><a href='switch.php'>Back</a></td> </tr> </table> </form> <?php break; default: echo "<table border='1'> <tr> <th>ID</th> <th>Date Of Birth</th> <th>Gender</th> <th>Title</th> <th>First Name</th> <th>Last Name</th> <th>Address Line 1</th> <th>Address Line 2</th> <th>City</th> <th>Postcode</th> <th>Contact No</th> <th>Email</th> <th>Additional Info</th> <th>Action</th> </tr>"; $result = mysql_query("SELECT * FROM project_data"); while($row = mysql_fetch_array($result)){ echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Date_Of_Birth'] . "</td>"; echo "<td>" . $row['Gender'] . "</td>"; echo "<td>" . $row['Title'] . "</td>"; echo "<td>" . $row['First_Name'] . "</td>"; echo "<td>" . $row['Last_Name'] . "</td>"; echo "<td>" . $row['Address_Line_1'] . "</td>"; echo "<td>" . $row['Address_Line_2'] . "</td>"; echo "<td>" . $row['City'] . "</td>"; echo "<td>" . $row['Postcode'] . "</td>"; echo "<td>" . $row['Contact_No'] . "</td>"; echo "<td>" . $row['Email'] . "</td>"; echo "<td>" . $row['Additional_Comment'] . "</td>"; echo "<td><a href='switch.php?action=edit&id=" . $row['ID']."'>Edit</a>  <a href='switch.php?action=delete&id=" . $row['ID']."'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; echo "<a href='switch.php?action=write'>Insert</a>"; } mysql_close($con); ?> Here is the completed code, but I still have a problem the page is coming up with an error: ( ! ) Fatal error: Cannot break/continue 1 level in C:\wamp\www\p1\switch.php on line 63 Call Stack # Time Memory Function Location 1 0.0009 413960 {main}( ) ..\switch.php:0 I cannot seem to figure it out, Can anyone help? Thanks Take a look he http://answers.yahooapis.com/AnswersService/V1/questionSearch?appid=YahooDemo&query=test That is a generated XML file from Yahoo's API. If you take a look, it has a node titled "Question". With PHP, how can I loop through each "Question" node and grab all of it's children as well? I tried this code, but apparently it did not work: $req = "http://answers.yahooapis.com/AnswersService/V1/questionSearch?appid=YahooDemo&query=php"; $res = file_get_contents($req); $xml = simplexml_load_string($res); foreach ($xml->xpath('//Question') as $question) { // "//question" means for each question node echo((string)$question['Subject']." - ".(string)$question['Content']); // you get all the child nodes in $question } I am pulling data from an sql data base. I want it to displayed (Item # | Error button | Link) however as its looping with a while on the database info I cant seem to get it to loop on the item number so it shows 1, 2, 3,.... on each row. Current coding is: while ($info = mysql_fetch_array($data)){ Print '<tr>'; Print"<td style='width: 15px;' class='cat-list'>$D</td>"; if ($info['id']==""){ Print"<td class='cat-list'> </td>"; }else{ Print"<td class='cat-list' style='width: 20px;'><img border='0' src='images/error.png' width='16' height='16'></td>"; } if ($info['link']==""){ Print"<td class='cat-list'> </td>"; }else{ Print"<td class='cat-list'><a target='_blank' href='".html_entity_decode(stripslashes($info[link]))."'>".substr(html_entity_decode(stripslashes($info[link])), 0, 85)."</a></td>"; } } How would I get the $D item to show 1 2 3... ect? Thanks, Jim ok, here is some code: Code: [Select] <?php // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM products"); while($row = mysql_fetch_array($sql)){ $product = $row["product"]; ?> <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#myform").validate({ debug: false, submitHandler: function(form) { // do other stuff for a valid form $.post('process.php', $("#myform").serialize(), function(data) { $("#price").load("index.php #price"); $('#results').html(data); }); } }); }); </script> <form name="myform" id="myform" method="POST" action=""> <input type="hidden" name="hiddenField" id="hiddenField" value="<?php echo $product; ?>" /> <input type="submit" name="submit" value="Submit" style="background-color:lightgreen; height:50px; width:100px;"> </form> <?php } ?>and here is process.php Code: [Select] <?php include_once("/connect.php"); ?> <?php $price=$_POST['hiddenField']; $sql = "INSERT INTO cart (price) VALUES('$price')"; $rs = mysql_query($sql) or die ("Problem with the query: $sql<br>" . mysql_error()); echo mysql_error(); ?>. what is happening is it is supposed to be submitting form without page reload.......but only the first one in the loop does it. I want all of them to do it. can anyone help me do this properly. ANYONE WHO CAN HELP ME WITH MY LOOP? THIS IS THE CODE THANKS IN ADVANCE.
<?php $testTwo = 'bakit ayaw po gumana paulit ulit bakit'; $testOne = 'bakit paulit ulit bakit ulit'; $tTwo = explode(' ', $testTwo); $tOne = explode(' ', $testOne); echo"SIMILARITIES:</br>"; foreach($tOne as $first) { foreach($tTwo as $second) { if($second == $first) { echo $second.' '; } } echo"</br>"; } ?> THIS IS THE OUTPUT SIMILARITIES: bakit bakit paulit ulit bakit bakit ulit THIS IS MY EXPECTING OUTPUT. bakit paulit ulit bakit ulit lang How come this doesn't loop? Everything is in the while loop.
My database connection is in the included file you see to begin the code which is what db_conx refers to just to be clear. Database connection is not an issue nor is getting values. I just get the first one and nothing more. No looping taking place here.
What I miss?
require('includes/db_connect.php'); $query = "SELECT * FROM events ORDER BY displayorder ASC"; $result = mysqli_query($db_conx, $query); while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { $pid = $row["id"]; $title = $row["title"]; $date = $row["date"]; $info = $row["info"]; $linkTxt = $row["linkTxt"]; $link = $row["link"]; $message = "<div id='events_holder'><table width='500' border='0' cellspacing='0' cellpadding='10'> <form action='edit_event_parse.php' method='post'> <tr> <td class='prayer_title'>Title:</td> <td><input type='text' name='title' class='admin_input' value='" .$title."' /></td> </tr> <tr> <td class='prayer_title'>Date:</td> <td><input type='text' name='date' class='admin_input' value='".$date."' /></td> </tr> <tr> <td class='prayer_title'>Link Text:</td> <td><input type='text' name='linkTxt' class='admin_input' value='".$linkTxt."' /></td> </tr> <tr> <td class='prayer_title'>Link URL:</td> <td><input type='text' name='link' class='admin_input' value='".$link."' /></td> </tr> <tr> <td class='prayer_title'>Event Details:</td> <td><textarea name='info' cols='20' rows='10' class='admin_area'>".$info."</textarea></td> </tr> <tr> <td><input type='hidden' name='pid' value='".$pid."' /></td> <td><input name='submit' type='submit' value='Edit Event' class='admin_submit'/></td> </tr> </form> </table> <p> </p> <hr /> <p> </p> </div>"; }Thanks! i have this bit of code which is supposed to do a while loop of rows from a database then inside that does a while loop of columns, so its basically creating a html table of dynamic data from mysql. the column loop works fine, but the row one isnt, it only goes through and echos out the first row then stops. ive spent hours now trying to see why and i cant see it hence my call for help. $totalrows = 4; $columns = 5; $columncount = 1; $rowcount = 1; echo "<table>"; while ($rowcount <= $totalrows) { echo "<tr>"; $sql = "SELECT * FROM ville_map WHERE id = '$rowcount'"; $sql_result = mysql_query($sql, $connection); while ($row = mysql_fetch_array($sql_result)) { $c1 = explode(',', $row["c1"]); $c2 = explode(',', $row["c2"]); $c3 = explode(',', $row["c3"]); $c4 = explode(',', $row["c4"]); $c5 = explode(',', $row["c5"]); while ($columncount <= $columns) { $arraynum = ${'c'.$columncount}; if ($arraynum[2] > 0) { $playerimage = '<img src="images/player' .$arraynum[2]. '.png"'; } else { $playerimage = ""; } echo '<td background="images/' .$arraynum[0]. '.jpg" width="50" height="50">' .$playerimage. '</td>' ; $columncount++; } } echo "</tr>"; $rowcount++; } ?> </table> any help would be much appreciated. ok I'm a bit stuck here were I would think something would work easily im getting an error. What I am trying to loop is.. $data="UPDATE ts12 SET djid='$_POST[TS0]' WHERE id=1"; $data="UPDATE ts12 SET djid='$_POST[TS1]' WHERE id=2"; This would go on for 84 id's. So I figured I could just loop this line so I only have to type it once. Right now I have it set up for just testing 7 ids and I have this code.... if ($edit == yes) { for ($i=0; $i < 7; $i++){ $n=($i+1); $data.="UPDATE ts12 SET djid='$_POST[TS$i]' WHERE id=$n"; } if (!mysql_query($data,$con)) die('Error: ' . mysql_error()); }else{} The error I am getting is... PHP Parse error: syntax error, unexpected T_VARIABLE, expecting ']' in dir/page on line 6 What am I doing wrong here? Hey, I have the following code that gets the name of the course from the XML. XML Snippet Code: [Select] <?xml version="1.0"?> <courses> <course> <id>70</id> <name>Marketing and Social Media</name> <dates> <date> <instance_id>6747</instance_id> <location /> <course_type>Day</course_type> <date_available>2011/07/13</date_available> <time_start>9:30</time_start> <time_end>17:00</time_end> <availibility>7</availibility> <running_dates> <running_date>2011/07/13</running_date> <running_date>2011/07/14</running_date> </running_dates> </date> </dates> </course> <course> </courses> PHP Code: [Select] $dom = new DomDocument(); $dom->load("courses.xml"); $xp = new domxpath($dom); $titles = $xp->query("/courses/course/name"); foreach ($titles as $node) { print $node->textContent . " "; } This prints out the course name fine. My question is how Can i get the rest of the data from the same query. For example why cant I do something like.. Code: [Select] ... foreach ($titles as $node) { print $node->name. " "; print $node->time_start. " "; print $node->course_type. " "; etc.. } So I want to loop through get the name and assign the rest of the nodes in there to variables or something. |