PHP - Loop Through Mysql Table And Perform Action On Row And Previous Row Field Php
I want to loop through a MySQL table, and perform a calculation based on the current row the loop is on and the previous loop.
Say my table has 2 columns - Film_id and FilmRelease, how would I loop through and echo out a calculation of the current FilmRelease and the previous row's column value? Thanks guys I have got to this stage, but for some reason it's not printing out anything Code: [Select] <?php mysql_connect("localhost", "****", "*****") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); $sql = mysql_query("SELECT FilmRelease FROM Films_Info") or die(mysql_error()); $last_value = null; while ($row = mysql_fetch_assoc($sql)) { if (!is_null($last_value)) { print date_diff($row['FilmRelease'], $last_value) . "<BR>"; } $last_value = $row['FilmRelease']; } Similar TutorialsHi,
I'm using an example below regarding a whole URL. I'm looking for some conditional PHP that will display certain content depending on if the URL contains the word 'liz' if not, display else.
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; if($host == 'liz-4.website.com') You would think the answer would be all over the Internet and easy to find, but it’s not. I have searched many times, and in all the multitude of search results I have still never found an adequate usable answer. The MOST you ever find is someone saying how easy it is with PHP, but they don’t tell you how, even when the person they are answering asks them (odd). You can be that one in a billion person who finally answers it for real and helps someone out. I have a simple HTML form with data fields first_name, last_name, email, phone, country, a few hidden inputs, and a single submit button, like so: (Please note: the method is GET, not Post.) <form action="https://MyDomainOnMyServer.com/MyPHPScript.php”> <input type="text" name="first_name" value="" /> <input type="text" name="last_name" value="" /> <input type="text" name="email" value="" /> <input type="text" name="phone" value="" /> <input type="hidden" name="type" value="type123"> <input type="hidden" name="project" value="new123"> <select required name="country"> <option value="">Choose your country</option> <option value="US">United States</option> <option value="CA">Canada</option> <option value="GB">United Kingdom</option> <option value="Many More">Many More Countries</option> </select> <input type="submit" value="Submit Form" /> </form> NOTE: Originally, the form action would have been: action="https://TheirExampleDomainOnTheirRemoteServer.com/TheirRemotePHPScript.php" name="form1234" Upon clicking the single submit button only, what I need to have happen is this: 1. Send me an email to whatever@whatever.tld containing all the form submission data 2. Place the form submission data into a MySQL database having the corresponding data fields 3. Send the form submission contents including the hidden input values to "https://TheirExampleDomainOnTheirRemoteServer.com/TheirRemotePHPScript.php" name="form1234" AS IF that had remained set as the original form action to begin with So basically what I’m trying to obtain is the cleanest possible PHP script that will do those three things, which is essentially what others have asked for over the years in search results I have found, but no one has ever provided it in a clear instance that works. If I can just see such a script, I should be able to see how it works and then do what I need. Thanks. Reply Hi
I am very new to PHP & Mysql.
I am trying to insert values into two tables at the same time. One table will insert a single row and the other table will insert multiple records based on user insertion.
Everything is working well, but in my second table, 1st Table ID simply insert one time and rest of the values are inserting from 2nd table itself.
Now I want to insert the first table's ID Field value (auto-incrementing) to a specific column in the second table (only all last inserted rows).
Ripon.
Below is my Code:
<?php $con = mysql_connect("localhost","root","aaa"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ccc", $con); $PI_No = $_POST['PI_No']; $PO_No = $_POST['PO_No']; $qry = "INSERT INTO wm_order_entry ( Order_No, PI_No, PO_No) VALUES( NULL, '$PI_No', '$PO_No')"; $result = @mysql_query($qry); $val1=$_POST['Size']; $val2=$_POST['Style']; $val3=$_POST['Colour']; $val4=$_POST['Season_Code']; $val5=$_POST['Dept']; $val6=$_POST['Sub_Item']; $val7=$_POST['Item_Desc']; $val8=$_POST['UPC']; $val9=$_POST['Qty']; $N = count($val1); for($i=0; $i < $N; $i++) { $profile_query = "INSERT INTO order_entry(Size, Style, Colour, Season_Code, Dept, Sub_Item, Item_Desc, UPC, Qty, Order_No ) VALUES( '$val1[$i]','$val2[$i]','$val3[$i]','$val4[$i]','$val5[$i]','$val6[$i]','$val7[$i]','$val8[$i]','$val9[$i]',LAST_INSERT_ID())"; $t_query=mysql_query($profile_query); } header("location: WMView.php"); mysql_close($con); ?>Output is attached. I was just wondering if it's possible to run a query on data that has been returned from a previous query? For example, if I do Code: [Select] $sql = 'My query'; $rs = mysql_query($sql, $mysql_conn); Is it then possible to run a second query on this data such as Code: [Select] $sql = 'My query'; $secondrs = mysql_query($sql, $rs, $mysql_conn); Thanks for any help I want to make a table for each paddler_id (Field) which exists into pushup Database My code for paddler_id = 1 is require "../sql.php"; $result = mysql_query("EXPLAIN pushup"); $r = mysql_query("SELECT * FROM paddlerinfo t1 LEFT JOIN pushup t2 on t2.paddler_id=t1.id LEFT JOIN practicedate t3 on t3.practice=t2.practice_id ORDER BY t1.firstname ASC "); $r1 = mysql_query("SELECT * FROM pushup where paddler_id =1 ORDER BY practice_id ASC "); echo "<table width='30%' border='1' cellpadding='0' cellspacing='0' style='font-family: monospace'>"; echo "<tr>"; echo "<td>DATE</td>"; while ($row = mysql_fetch_array($result)) { if (in_array($row["Field"], array("paddler_id", "practice_id", "p_id"))) continue; echo "<td>",($row["Field"]), "</td>"; } echo "</tr>"; while (($data = mysql_fetch_array($r1, MYSQL_ASSOC)) !== FALSE) { unset($data["p_id"],$data["paddler_id"]); echo "<tr>"; foreach ($data as $k => $v) { echo "<td>"; echo"$v"; echo "</td>"; } echo "</tr>"; } echo "</table>"; mysql_free_result($result); mysql_free_result($r); mysql_free_result($r1); mysql_close(); The problem is on line $r1 = mysql_query("SELECT * FROM pushup where paddler_id =1 ORDER BY practice_id ASC "); I want to put something which makes it paddler_id = X where x = 2,3,4,5... and x exists in database and do not print twice the x (because that database may have rows where x = same id How do i do that?? Hi, when I submit a form I want to update a field in the "daterange" table. In the form the id field (RID) from "daterange" is selected prior to submit. Basically, I want to change the STATUS field to B (from A). The structure of daterange table is as follows: RID (key field) DEND MONTH DATE SITE PRICE STATUS What is the easiest way to accomplish this? Here is the code from the form (if it will help)... Code: [Select] <?php //************************************** // Page load dropdown results // //************************************** function getTierOne() { $result = mysql_query("SELECT DISTINCT MONTH FROM daterange") or die(mysql_error()); while($tier = mysql_fetch_array( $result )) { echo '<option value="'.$tier['MONTH'].'">'.$tier['MONTH'].'</option>'; } } //************************************** // First selection results // //************************************** if($_GET['func'] == "drop_1" && isset($_GET['func'])) { drop_1($_GET['drop_var']); } function drop_1($drop_var) { include_once('db.php'); $result = mysql_query("SELECT * FROM daterange WHERE DEND > DATE(NOW()) AND STATUS='A' AND MONTH='$drop_var' ORDER BY DATE, SITE") or die(mysql_error()); echo '<select name="RID"> <option value=" " disabled="disabled" selected="selected">Choose a Reservation</option>'; while($drop_2 = mysql_fetch_array( $result )) { echo '<option value="'.$drop_2['RID'].'">'.$drop_2 ['DATE']. ', '.$drop_2 ['SITE']. ', '.$drop_2 ['PRICE'].'</option>'; } echo '</select> '; echo "<br />"; echo "</select><p align=left><label><font size=\"2\" face=\"Arial\">First Name: <input type=\"text\" name=\"FNAME\" size=\"50\" maxlength=\"50\" tabindex=\"1\"<br />"; echo "<p align=left><label>Last Name: <input type=\"text\" name=\"LNAME\" size=\"50\" maxlength=\"50\" tabindex=\"2\"<br />"; echo "<p align=left><label>Address Line 1: <input type=\"text\" name=\"ADDR1\" size=\"50\" maxlength=\"50\" tabindex=\"3\"<br />"; echo "<p align=left><label>Address Line 2: <input type=\"text\" name=\"ADDR2\" size=\"50\" maxlength=\"50\" tabindex=\"4\"<br />"; echo "<p align=left><label>City: <input type=\"text\" name=\"CITY\" size=\"50\" maxlength=\"50\" tabindex=\"5\"<br />"; echo "<p align=left><label>State (abbrev.): <input type=\"text\" name=\"STATE\" size=\"2\" maxlength=\"2\" tabindex=\"6\"<br />"; echo "<p align=left><label>Zip Code: <input type=\"text\" name=\"ZIP\" size=\"5\" maxlength=\"5\" tabindex=\"7\"<br />"; echo "<p align=left><label>Contact Phone Number: (<input type=\"text\" name=\"PHONE1\" size=\"3\" maxlength=\"3\" tabindex=\"8\""; echo "<label>)<input type=\"text\" name=\"PHONE2\" size=\"3\" maxlength=\"3\" tabindex=\"9\""; echo "<label>-<input type=\"text\" name=\"PHONE3\" size=\"4\" maxlength=\"4\" tabindex=\"10\"<br />"; echo "<p align=left><label>Email: <input type=\"text\" name=\"EMAIL\" size=\"50\" maxlength=\"50\" tabindex=\"11\"<br />"; echo '<input type="submit" name="submit" value="Book Now!" /><br />'; echo '<input type="reset" name="submit" value="Reset" /><br />'; } ?> I'm trying to create a search form where members of a site can search other members whose user information is stored in a mysql database. One of the search parameters is gender which searches for records from a column in the database called gender where members can either have the value "man" or "woman". The user can choose between 3 options from a pull down menu named gender (man, woman, both). Then I'm thinking about using a select query to search the database upon submission of the form, which goes something like this: Code: [Select] $sql= SELECT* FROM members WHERE gender ='{$_POST['gender']}' ; Now I can easily assign the value "man" for the option man in the pull down menu and "woman for the option woman, to match their corresponding names in the database. The problem lies with the "both" option which has to be either man or woman. I'm thinking about assigning it the value "man OR woman" so that when the form is submitted, the query would read: SELECT*FROM members WHERE gender ='{$_POST[man OR woman']}; I just don't know if this would be a right usage of the OR keyword and if such a query would work. Before trying it out, I'd like to know if this makes any sense and if not, what's an alternative way to work around this? Hi, I'm trying to get a number of different keywords from many table fields, and i want to count them according to the occurrence of each keyword, so it should be like (keyword1 occurred 10 times for example ). the problem is when i use count in for loop: Code: [Select] $tags = explode("|", $row[0]); $i=1; $count = 0; for($i; $i<count($tags); $i++){ print "$count {$tags[$i]}<br />"; $count++; } it counts the occurrence of key words in each field separately from the rest (i.e. Code: [Select] 0 day 1 night 0sun 1 moon 0 clouds 1 rain ) 2 keywords in each field (some other fields got more). I want the count to be more like: Code: [Select] 1 day 2 night 3 sun 4 moon 5 clouds 6 rain thanks Here is one part of php code. Here I try to show in last 1 month which dates exactly user made orders. When I run the SQL code in command prompt it works. I think problem is in while loop. Thanks beforhand for contribution. $query = "SELECT Date FROM orders WHERE User='$username' and DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= Date;"; $result = mysql_query($query,$link_id) or die (mysql_error()); echo "<table border='1'> <tr> <td> Last 1 month's orders:</td> </tr>"; while($order_date = mysql_fetch_row($result)) { echo "<tr>"; echo "<td>" . $order_date[0] . "</td>"; echo "</tr>"; } echo "</table> I am trying to place a unique number into a mysql table. Currently, my code generates a random number, then is supposed to scan through the table for that number. If the code finds that number already in the table, it generates a new random number and repeats the process. I have commented my code for the purpose of this help forum: Code: [Select] $result = mysqli_query($link,"SELECT * FROM testTable"); do { $end = true; //prepares end of loop $idNum = rand(1,10); //rand(1,999999); <-- for testing purposes I have reduced the number generated $idNumTx = (string)$idNum; //loop through the rows while ($row = mysqli_fetch_assoc($result)) { if ($row['idNum'] = $idNum) //check if the random number equal to this row { $end = false; //prep end of loop repeat echo $idNumTx; //display rand number that failed for testing purposes echo " NO! "; //display error for testing purposes } } } while(!$end); I know I must be doing something wrong, as when I run this, it runs the if statement within the while loop always executes, and I get an output like: Code: [Select] 1 NO! 1 NO! 1 NO! 1 NO! 1 NO! 1 NO! 5 Win Win is when it places the value before it, in this case 5, into the table. However, the value of 5 might already be in the table and it doesn't seem to matter. I execute the code multiple times, and it seems to increase the number of "# NO!" almost (but not every) time. However, each time ALL of the "# NO!" are the same #, and the "# Win" just seems to be random (as it should be, but not unique). Checking the table after shows me random numbers between 1 and 10 (as it should) in the correct field, but the are not unique. (Ex/ Both row 1 and 5 could have the same value, say 6) I'm hopefully doing something simple wrong, so someone please point it out to me Hello, my name Rustam, do the work - PHP MySQL JS jQuery and Yii framework.
Quickly. This is my first real jump into PHP, I created a small script a few years ago but have not touched it since (or any other programming for that matter), so I'm not sure how to start this. I need a script that I can run once a day through cron and take the date from one table/filed and insert it into a different table/field, converting the human readable date to a Unix date. Table Name: Ads Field: endtime_value (human readable date) to Table Name: Node Field: auto_expire (Converted to Unix time) Both use a field named "nid" as the key field, so the fields should match each nid field from one table to the next. Following a tutorial I have been able to insert into a field certain data, but I don't know how to do it so the nid's match and how to convert the human readable date to Unix time. Thanks in advance!. Why won't this increment the database field by 1? $sql2="update 'users' set 'TimesLoggedOn' = 'TimesLoggedOn' + 1 where 'BarcodeID' = $barcode"; mysql_query($sql2); Thanks! I am currently working on an application to schedule conferences. I have everything completed to create a conference including start time and duration. The problem I have now though is how do I monitor MySQL for the conferences that are created and perform actions when the duration of the conference is up? I was looking into MySQL events but those seem to be only internal events and are not able to execute anything outside of that. I was thinking about a infinite loop that queries MySQL and sleeps for one second but thought that this might be a lot of load on the server. Any thoughts I really appreciate it. This way seems not good. <?php $username ="MANCENT"; $user_input_x = _xmouse.movement ;//FLASH $user_input_y = _ymouse.movement ;//FLASH while(true) { $updatepos = mysql_query("UPDATE users_float SET user_input_x = '$user_input_x' , user_input_y = '$user_input_y' WHERE username = '$username'"); } This way works but whats a better way? ?> This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=358560.0 Hi there, I was wondering if it was possible to change a forms action field with PHP. Basically, if there are several checkboxes for the user to select and a specific checkbox was selected, how would I redirect users to an alternative webpage on submit? I've been playing around with several methods, and one of them was: Code: [Select] <?php if(isset($_POST['Example']) && $_POST['Example'] == 'FormValueGoesHere') { echo ('./page1.php'); } else { echo ('./page2.php'); } ?> But the form action would only display page1.php even with nothing selected. Maybe I'm just being stupid because it's getting late, but I'd appreciate any assistance. Thanks I use foreach to echo a list of tags come out of an array as Code: [Select] foreach ($array as $tag) { echo "$tag,"; }This produces "tag1,tag2,tag3,". The problem is the last ",". How to tell the foreach loop to avoid including "," after the last sequence? Hi all, I have a fairly simple problem, but i cannot get to it. I have a page, that displays one (1) record at the time, and I want to have forward and back buttons (links) at the top and bottom of the page. Can anyone help me with that??? Here is a part of my code. Code: [Select] <form method="get"> <table border="0" width="90%" cellpadding="2" cellspacing="2" align="center"> <tr><td align="center"> Select first letter or kind of the story: <input type="hidden" name="link" value="stories.php"> <input type="hidden" name="Pripadnost" value="3"> <select name="letter" onchange="this.form.submit()"> <?PHP $letter = trim($_REQUEST["letter"]); ?> <option value="Sve" <?PHP echo $letter == "All" ? " selected " : "" ; ?>>All</option> <?PHP include_once ("/includes/databaseMySQL.php"); mysql_set_charset('utf8'); $SQL = "Select distinct ucase(left(title,1)) as sl FROM tblStoryes order by sl"; $rs_data = mysql_query($SQL) or die(mysql_error()); while ($row_data = mysql_fetch_assoc($rs_data)) { ?> <option value="<?PHP echo $row_data["sl"]; ?>"<?PHP echo $letter == $row_data["sl"] ? " selected " : "" ; echo ">".$row_data["sl"] ;?></option> <?PHP // ****************** Listed all the first letters of the titles into a drop box. *********************** // } ?> </select> Bosnian <input type="checkbox" name="Bosnian " <?PHP echo isset($_REQUEST["Bosnian "]) ? "checked=checked" : "" ;?> onclick="this.form.submit()" /> Turkish <input type="checkbox" name="Turkish " <?PHP echo isset($_REQUEST["Turkish "]) ? "checked=checked" : "" ;?> onclick="this.form.submit()" /> Arabian <input type="checkbox" name="Arabian " <?PHP echo isset($_REQUEST["Arabian "]) ? "checked=checked" : "" ;?> onclick="this.form.submit()" /> </td></tr> </table> <table border="0" width="90%" cellpadding="2" cellspacing="2" align="center"> <?PHP $id = -1; $rs_data = mysql_query("Select max(id) as maxid from tblStories WHERE aktivno = True") or die(mysql_error()); $row_data = mysql_fetch_assoc($rs_data); $maxid = (int) $row_data["maxid"]; // ****************** Found the largest ID from the table. *********************** // $SQL = "SELECT * FROM tblStories WHERE aktivno = True "; if ( strlen($letter) < 3 && strlen($letter) > 0 ) { $SQL = $SQL . " and ucase(left(`title`, 1)) = '" . $Letter . "'"; } $lang = 0; $lang = $lang + isset($_REQUEST["Bosnian"]) * 4; $lang = $lang + isset($_REQUEST["Turkish"] ) * 2; $lang = $lang + isset($_REQUEST["Arabian"] ); switch ($lang) { case 6: // bos i tur $SQL = $SQL . " and ( Bosanski = 1 or Turski = 1 ) "; break; case 5: // bos i ar $SQL = $SQL . " and ( Bosanski = 1 or Arapski = 1) "; break; case 4: // bos $SQL = $SQL . " and Bosanski = 1 "; break; case 3: // tur i ar $SQL = $SQL . " and ( Turski = 1 or Arapski =1) "; break; case 2: // tur $SQL = $SQL . " and Turski = 1 "; break; case 1: // ar $SQL = $SQL . " and Arapski =1 "; break; } if (trim($_REQUEST["DBid"]) <> "" or $_REQUEST["DBid"] > 0) $SQL = $SQL . " and id > " . trim($_REQUEST["DBid"]) ; $SQL = $SQL . " LIMIT 1"; //order by naslov $rs_data = mysql_query($SQL) or die(mysql_error()); echo "<!--" . $SQL . " : Jezik je:" . $lang . "-->"; while ($row_data = mysql_fetch_assoc($rs_data)) { $id = (int) $row_data["ID"]; ?> <tr> <td align="center" colspan=3> <h3><?PHP echo $row_data["Title"];?> </h3> </td> </tr> <tr> <td width=20%> </td> <td align="justify" width=60%> <?PHP echo $row_data["tekst"]; // tekst = text, but not keyword ?> </td> <td width=20%> </td> </tr> <?PHP } ?> </table> <p> <?PHP // ****************************** HEEEEELLLLPPPPPP ******************************* // ?> <table border="0" width="90%" cellpadding="1" align="center"> <tr> <td align="center"><a href="?DBid=1<?PHP foreach($_REQUEST as $arr => $elem){ if ( $arr != "DBid") echo "&" . $arr . "=" . $elem; } ?>"> <<</a></td> <td align="center"><a href="?DBid=<?PHP echo $_REQUEST["id"]; foreach($_REQUEST as $arr => $elem){ if ( $arr != "DBid") echo "&" . $arr . "=" . $elem; } ?>" ><</a></td> <?PHP if ( $maxid <= $id ) { ?> <td align="center">></td> <td align="center">>></td> <?PHP } else {?> <td align="center"><a href="?DBid=<?PHP echo $id ; foreach($_REQUEST as $arr => $elem){ if ($arr != "DBid") echo "&" . $arr . "=" . $elem; } ?>">></a></td> <?PHP if ( ($id + 9) < $maxid ) { ?> <td align="center"><a href="?DBid=<?PHP echo $id+10 ; foreach($_REQUEST as $arr => $elem){ if ($arr != "DBid") echo "&" . $arr . "=" . $elem; } ?>">>></a></td> <?PHP } else {?> <td align="center">>></td> <?PHP }?> <?PHP }?> </tr> </table> </form> |