Queries Outside Loops?
It was suggested to me that to speed up some code I have for a multiple select box that I place my queries outside my loops. I can't say I have the foggiest clue on how to do that.
My original code looked like: PHP Code:
View Complete Forum Thread with Replies
Sponsored Links:
Related Messages:
Do Queries On XML Similar To The Way That You Do Queries On MYSQL And Then Use The Results.
I am fairly familiar with PHP and MYSQL and I know a little about XML. I would like to know if it is possible to do queries on XML similar to the way that you do queries on MYSQL and then use the results. An example of a simple PHP/MYSQL setup. <?php $user="username"; $host="sqlserver"; $password="password"; $database="database"; $connection = mysql_connect($host,$user,$password) or die ("Couldn't connect to the server"); $db = mysql_select_db($database,$connection) or die ("Couldn't connect to the server"); $query = "SELECT * FROM database WHERE(Criteria)"; $result = mysql_query($query) or die ("Couldn't execute query."); while ($row = mysql_fetch_array($result)) { extract($row); echo"Set of instructions to carry out for each row in the result"; } ?> The question is, is it possible to do similar things with PHP/XML? Lets say that I have an XML file located at a certain URL and I want to run a query on it and use the results. 1. Is it possible? 2. What is the easiest way to do it? 3. How would I translate the above code to work with XML? 4. Is ther resource on the web on how to structure the queries?
View Replies !
View Related
Nesting Result Loops Within Result Loops Possible?
I'm looping through a query, and inside the while result output, I am trying to loop through a second query (created from the 1st query's row results). I have checked that both queries return info. I can see that the 1st while result output works fine, but when I get to the inner while result output, it never returns any rows (nothing in the while loop evaluates). So, IS it possible to nest one query's output loop within another, or am I doing something wrong?
View Replies !
View Related
For Loops Vs While Loops
Seems like every day I read 2-3 posts espousing the use of Foreach over While and vice versa. Time to set the record straight... http://www.php.lt/benchmark/phpbench.php (not my page, but it was far easier than coding it myself) And the winner is... For loops. There you have it. Concrete proof, instead of conjecture. For loops are faster. I guess I'll have to code up some tests to see if it's faster to grab the count from a mysql query and use a for loop vs using a while loop to iterate over a returned record set. Feel free to comment, but please, cite refrences if you wish to contradict what I have written. I'm tired of hearing "facts" that are unsubstantiated.
View Replies !
View Related
Using While Loops
Does anyone know how collect user information from a database for each user, perform an operation on it, then output each user's information as a seperate variable? I have it to the point where I set up a while loop that retrieves each user's information but I can't automate the setting of the unique variables.
View Replies !
View Related
Using 2 Loops With MAX()/MIN()
I am developing a simple messageboard that will list threads. There will be the first post of the thread and directly underneath will be the last reply to that thread. My problem is that instead of the last reply, the last post in the db is being displayed under all of the threads. actionID is the auto incremented primary key. subjectID is an integer that is the same for a post and all of its replies. A new post will have the next incremented integer. Code:
View Replies !
View Related
Using Loops
i tried using one for trying to display how many pages of message you have in your inbox.. (this froze my computer) heres some of my code echo "<form METHOD=get><input type=hidden name=view value=Message><table width=70% border=0 cellspacing=1 cellpadding=0> <tr><td background=images/forum_board.jpg height=25 width=0></td><td background=images/forum_board.jpg height=25 width=33%><b><center>Subject</center></b></td><td background=images/forum_board.jpg height=25 width=33%><b><center>From</center></b></td><td background=images/forum_board.jpg height=25 width=33%><b><center>Date</center></td></tr>"; $slimit = $action*15; $llimit = ($action+1)*15; $pms = mysql_query("SELECT * FROM pm WHERE pm_to='$get_user[id]' ORDER BY id DESC LIMIT $slimit, $llimit");..............
View Replies !
View Related
While Loops
I have to put a while loop in an assignment and I was wondering if someone could explain what it does and possibly give me an example then I can at least attempt it on my own.
View Replies !
View Related
IF Loops Help
I read a little tutorial how to start with if loops and now I am stuck with this one: Quote: if(isset($_POST['Submit'])){ //Shortening Variables $userid = $_POST['userid']; $fname = $_POST['fname']; $lname = $_POST['lname']; $password = $_POST['password']; $vpassword = $_POST['vpassword']; $ad1 = $_POST['ad1']; $ad2 = $_POST['ad2']; $zipcode = $_POST['zipcode']; $hphone = $_POST['hphone']; $mphone = $_POST['mphone']; $pemail = $_POST['pemail']; ...................
View Replies !
View Related
If..else Loops
I need somebody to repair the looping..It only loops 2 times. I wanted it to loops at least 5 times and display only the last row for each employee. All the data i get from database HPTRX tables. Code:
View Replies !
View Related
Loops
I'm building a form using the <select> tags to display a group of categories and sub categories like so: Parent --->Sub cat --------> Sub sub cat Parent 2 ---->Another sub cat I've written the following script, but it seems inefficient to keep writing it out multiple times for each sub category but I can't figure out how I'd write a loop to do this automatically. Code:
View Replies !
View Related
While Loops?
I'm trying to while loop using a flat txt file and write the values out into a form. I have 50-58 values to echo. Am I going to have to echo each line of html. Or is there a way to make this work between the php. Right now it wants to write the whole table over and over. Is my logic screwed up? Code:
View Replies !
View Related
For And While Loops
Ive got a javascript that creates a slideshow of some images that i specify in such way: fadeimages[0]="photo1.jpg" fadeimages[1]="photo2.jpg" fadeimages[2]="photo3.jpg" ... and so on I have been trying to make a php script that reads a folder and searches for images. with those images, it uses a loop to print each image in such way so that the javascript will be able to work! Code:
View Replies !
View Related
Need Help W/using Loops With Templates
I'm starting to use templates for my site and was curious about how they handled loops. For example, let's say I have a HTML table like this: <table width="750" border="0"> <tr> <td>{ARTIST}</td> <td>{SONG_TITLES}</td> </tr> <tr> <td>{ALBUM_TITLE}</td> <td> </td> </tr> <tr> <td> </td> <td> </td> </tr> </table> And the PHP file then calls records from the database for each placeholder in a while loop like this... $result=mysql_query("select * from music where id = '$album_id'"); while ($row = mysql_fetch_array($result)) { $album_id=$row['album_id']; $artist= $row['artist']; $album_title= $row['album_title']; $song_title=$row['song_title']; } Normally, I would do something like this in the php file to point the data to the placeholder.... $myTemplate->tag("Site_title",gettitle()); So...my question is...where would I place the call to the database and the while loop and point each placeholder. Would I do something like this... getmusic(); $myTemplate->tag("Album_Title",$row['album_title']); OR $myTemplate->tag("Album_Title",$album_title); If anyone has experience with this, please help! Thanks!!
View Replies !
View Related
Mysql_data_see For For() Loops?
Am I right to say mysql_data_seek allows me to walk through a database return as I would an array? for ($1=0; $i < $num; $i++) { mysql_data_see($result, $i); $row = mysql_fetch_array($results); } I still haven't solved the problem I mentioned in another post (I can only get the first row back from my returns), so I'm tempted to go this route.
View Replies !
View Related
Goto Loops?
I had someone ask me this at work and I had no clue if it existed in PHP, so I'm wondering if any of you know. In most scripting languages, they have a goto command (as in mIRC) and you can specify different sections, i.e.PHP Code:
View Replies !
View Related
Do...while Loops Forever
While attempting to alleviate a persistent but not consistent problem with my database operation (DB2) I coded the do...while loop around my odbc_connect. When $dbConn is false the first time, the program never returns. This problem repeatable with or without the @ on the odbc_connect call. I would someone pointing out my stupidity. I did RTFM, including the examples, and was no further enlightened. Thanks in advance. function conn() { global $schema, $database; $schema = "is3"; $database = "animalst"; $dbuser = "db2inst1"; $dbpwd = "db2rules"; $ct = 0; do { $dbConn = odbc_connect($database,$dbuser,$dbpwd); $ct += 1; if($dbConn==False) { sleep(1); } } while(($dbConn==False) && ($ct<5)) ; if ( $dbConn==False) { printf("<br>The odbc_connect parameters were:[%s] [%s] [%s]<br> " , $database,$dbuser,$dbpwd); printf("<br>Tried %d times<br> ",$ct); showerror(); die("could not connect to $database"); } return $dbConn; } ?
View Replies !
View Related
Code WHILE Loops
A script is written to query a db and print the result on the screen. $result = mysql_query("SELECT * FROM events",$db); $myrow = mysql_fetch_array($result); $type = $myrow['type']; echo "Type = $type"; while ($myrow = mysql_fetch_array($result) { $type = $myrow['type']; echo "Type = $type";} Question: Is there a cleaner way of coding this loop (which works fine, btw)? If I don't include the first fetch_array & echo statement, the first row of the table is not printed to the screen. Using: while ($result) makes an endless loop (gets stuck on the first record). Same thing for while ($myrow). I don't mind coding it like my example above, but it seems like this is a convoluted way of doing things.
View Replies !
View Related
Forms + While Loops
i have a 5x5 grid filled with 25 Option Buttons, now i want to make a loop that circles through them to see which one is selected and then compares that to a number in the database to see if something should be done. Is this possible? if so how?
View Replies !
View Related
DBs, Arrays And Loops
I've got a database that I'm pulling records from. That works. I need to loop through each selected record, put it into an array and then output that array to a function. I can loop. I can array. I can output a manually contructed array. I just cannot get the array to loop through the db records. Code:
View Replies !
View Related
For Loops - Uses Over And Over For Incrementing
I'm trying to0 learn PHP and keep running into what looks like an example. I'm not sure though if the $i is suppose to mean somthing of if it is just an example. Here is the common thing I am seeing for ($i o; $i<3; $i++ ) So am I correct in thinking this just an example that this author uses over and over for incrementing, or is it a predefined variable in PHP.
View Replies !
View Related
While Loops Does Not Display The First Row
This while loops does not display the first ror it gets from the mysql query. Can anyone tell me why? PHP Code: $result = mysql_query("SELECT * FROM users"); if(!$result) { Â Â Â Â $error = true; Â Â Â Â $errormsg = "<p>Error running query: ".mysql_error()."</p>"; } else { Â Â Â Â $row = mysql_fetch_array($result); Â Â Â Â if(!$row) { Â Â Â Â Â Â Â Â $error = true; Â Â Â Â Â Â Â Â $errormsg = "<p>Could not get users: ".mysql_error()."</p>"; Â Â Â Â } else { Â Â Â Â Â Â Â Â while($row = mysql_fetch_array($result)) { Â Â Â Â Â Â Â Â Â Â Â Â if($row["permissions"] == 1) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $user_permissions = "Normal"; Â Â Â Â Â Â Â Â Â Â Â Â } elseif($row["permissions"] == 2) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $user_permissions = "Superuser";.....
View Replies !
View Related
Foreach, While Loops?
I have an array which consists of some MySQL data. What I'm trying to do is output the arrays 10 at a time... so here is a dirty example of what I'm looking for. for each 10 results, do this: <begin tag> result1, 2, 3, 4, 5, 6, 7, 8, 9, 10 </end tag> <begin tag> results11, 12, 13, 14, 15, 16 </end tag> So for every "10 results", it needs to start a new set of tags. This will be in CSS div's.
View Replies !
View Related
PHP Loops Problem.
I have set up the database, and in the database I have 2 tables so far, one is for the pictures categories which has the names of the categories, and the urls, and the other is for the pictures which has the title of the pictures, and filenames and description, so I linked them both with a associated primary/foreign key relationship. Code:
View Replies !
View Related
Loops And Variables
I'm developing a small application that needs to repeated call on a series of recordsets that are similarly named. What I would like to do is call these recordsets in turn, display the contents. Repeat for each of the of them. Assuming I have recordsets name rset0, rset1 ... rset9. The recordsets themselves have been declared earlier. The following code doesn't work obviously but i hope shows what I am trying to achieve. Code:
View Replies !
View Related
Nested Loops
I think nested loops will do what I want, but I can't seem to get them right. I have two tables, members and casts. I run the following query which gets me the data I want to display using the following php: Code:
View Replies !
View Related
Double While Loops
I'm trying to fill a select box in the update form so that multiple values in the select box are highlighted if the user previously selected multiple values, one if they only selected one value, and zero if they didn't select any values. The main problem I'm having writing this code is that I need to draw the values for the select box from two tables: the sections table which populates the entire possible range of select box values and the sections_x table which includes only the values that have been selected. What I've come up with so far is a double while loop: while ($row = mysql_fetch_array($result5)) { $section_id = $row['section_id']; $section = $row['section']; while ($row = mysql_fetch_array($result8)) { $section_id_x = $row['section_id']; $date_id_x = $row['date_id']; if ($section_id == $section_id_x) { $option_block5 .= "<option value="$section_id" selected="selected">$section</option>"; } else { $option_block5 .= "<option value="$section_id">$section</option>"; } } } However, the select box is just showing up blank.
View Replies !
View Related
Loops In <tr> Rows
I have numerous images with different categories in a db. On the page I want to show a maximum of 4 <td> rows, then if there are more images in the category it starts a new <tr> and maximum 4 <td> rows. Say for example there are only 6 images for a category I would like the left over <td> rows to be <td> </td> Can this be done? I currently use while ($myrow = mysql_fetch_array($result)) to show the results.b Hope that makes sense?
View Replies !
View Related
Arrays, Loops
i hv a code which allows a user to enter personal details. on top of that, it prompts user to enter no of entries of other names btwn1-10. upon entering eg 5, 5 sets of drop down lists will be shown for the user to select 5 different names. after tht, these records will be stored in mysql. each name has 3 fields (id, name, add). as i did not know how to use for loops to rotate the records to be stored in the database, i used the long method of creating 10 fields in the database and simply store them inside. eg........ table_id_1, table_name_1, table_add_1, table_id_2......n so on...till 10. what should i do? it gets worse this method cos later part, i need to extract all records and do more queries.
View Replies !
View Related
Arrays And Loops
The following code is looping through a database to write out an shopping cart order into an array. I display the array outside of the loop elsewhere in my page without having to do things within the same loop. For instance: foreach ($product as $key => $value) {......
View Replies !
View Related
Multiple Loops
PHP Code: <?php echo '<script language="Javascript" type="text/javascript">' $i = 0; foreach ($catArray as $temp){ $name = "v_".$i; echo ''.$name.' = new Array();' $i++; } $querry = "SELECT * FROM $playlists ORDER BY 'id' ASC"; $selection = mysql_query($querry); while($rowNum = mysql_fetch_array($selection)) { $title=$rowNum['name']; $url=$rowNum['url']; $cat=$rowNum['categorie']; .........
View Replies !
View Related
A For Loop That Loops 1.5 Times
Below is some code from a page of mine that is supposed to process form data. The data is made up of checkboxes that a user randomly selects when they want to delete a file from a list of files. dbmysqlconnect_pgs(); for ($x = 0; $x <= count($checkbox); $x++) { $query2 = "SELECT name FROM proposalinfo WHERE propid='$propid' "; $results2 = mysql_query($query2) or die("Error in query 2"); while($query_data = mysql_fetch_array($results2)) { $name = $query_data["name"]; } $query4 = "SELECT filename FROM files WHERE fileid='$checkbox[$x]' AND propid='$propid' "; $results4 = mysql_query($query4) or die("Error in query 4"); while($query_data = mysql_fetch_array($results4)) { $filename = $query_data["filename"]; } //THIS IS WHERE THE ARCHIVE OF THE OLD FILE IS PROCESSED $query3 = "INSERT INTO archives (date, userid, propid, filename, fileid) VALUES (NOW(), '$userid', '$propid', '$filename', '$checkbox[$x]') "; $results3 = mysql_query($query3) or die("Error in query 3"); $query4 = "SELECT date FROM archives WHERE fileid='$checkbox[$x]' AND propid='$propid' "; $results4 = mysql_query($query4) or die("Error in query 4"); while($query_data = mysql_fetch_array($results4)) { $date = $query_data["date"]; } //THIS IS WHERE THE RENAME OF THE FILE IS PROCESSED $oldfilename=$filename; $oldname="./" . $name . "/" . $oldfilename; $newfilename=$oldfilename . $date; $newname="/overflow/pgs_archives/" . $name . "_archive/" . $newfilename; copy("$oldname", "$newname"); unlink("$oldname"); //THIS IS WHERE THE DELETE OF THE DB DATA IS PROCESSED $query5 = "DELETE FROM files WHERE fileid='$checkbox[$x]' AND propid='$propid' "; $results5 = mysql_query($query5) or die("Error in query 5"); $query6 = "DELETE FROM fileinfo WHERE fileid='$checkbox[$x]' "; $results6 = mysql_query($query6) or die("Error in query 6"); } indentheader(ཐ',',ཐ','header3','The selected files have been deleted from the system.'); The problem I am having is that this will only execute 1.5 times before failing. The failure is at query3. Basically if you selected two or more checkboxes you will only delete one of the entries. Query3 runs by itself just fine and works the first time. Anyone have any ideas?
View Replies !
View Related
Loops In Template Class
I wrote a template class and now when i go to parse data that should loop, it will just parse the last entered field [code}$map = $row['map']; $win = $row['result']; $output = " <table border="0" width="100%" id="table3"> <tr> <td bgcolor="#657685" width="26%" align="center"><b> <font face="Verdana" size="1" color="#FFFFFF"> $row[team]</font></b></td> <td bgcolor="#657685" width="38%" align="center"> <font color="#FFFFFF" size="1" face="Verdana"> $row[map]</font></td> <td bgcolor="#657685" width="17%"> <p align="center"> <font size="1" face="Verdana" color="#FFFFFF">$row[roundwon] - $row[roundlost]</font></p></td> <td bgcolor="#657685" width="11%"> <p align="center"><b> <font color="$color" size="2" face="Verdana">$row[result]</font></b></p></td> </tr> </table></td> </tr> "; }; [/code] That is the code that fetches the information. and here is the info for the template array "MATCHES" =>"$output", Also, the rest of the template class works fie but i don't know how im going to make it "loopable" as such.
View Replies !
View Related
Objects, Arrays, And Loops
I'm working on an auto-schedular for my college's Music Festivals. I just started on this tonight. I'm starting by reading all the information into variables. For starters, I have a School class. The script should read data from the database into School objects for later use. I've never used objects in PHP before, but I've used them in several other languages. I read data from the database into the School objects using a loop, with a loop invariant called $schoolCount. I created a print statement so that I could see if the data was being correctly assigned to the School objects, but I think it's printing the value of the loop invariant instead of the school name like I want. I'm not sure why. Here's my code:
View Replies !
View Related
Conditional Statements And Loops
Not long ago I was asked to give PHP lections to some private IT school. I'm on second lection now, and will be teaching my :) students conditional statements (if..else and switch) and loops (while and for). That was my story, now the question. Can you give me an advise on some geeky use of those statements to stir up some enthusiasm in students.
View Replies !
View Related
For Loops.....code Breaks
I'm trying to read an excel worksheet (with more than 5000 rows and 30 columns) using PHP. I'm using the "excelreader" script I found over the web - http://sourceforge.net/projects/phpexcelreader/. Seems to me, the script reads the values in all cells correctly and stores the total number of rows and columns existing in the worksheet (For example, in the excel file I'm using - total rows: 5375 and cols: 37). The problem is when I try to print the values as a HTML table (using two for loops), the script prints only first 236 rows and stops execution. The following is the code snippet I use: for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { echo "<TR>"; for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { echo "<TD>".$data->sheets[0]['cells'][$i][$j]; } echo "<BR>"; } I've also set the script execution time to 5-10 mins using "set_time_limit" function, but the script still ends after printing 236 rows (takes less than a minute). I spot checked the cell values by printing values from 300 to 500, 5200 to 5375 etc. The values seems to okay and stored correctly in the arrays. From what I understood, the for loops doesn't execute completely and I don't understand why? I've also created a small test script to see if the loops work and the following code breaks...I don't see a output. <?php set_time_limit(300); for ($i = 1; $i <= 5000; $i++) { for($j = 1; $j <= 40; $j++) { $data[$i][$j] = "mycell - $i$j"; //. ($i * $j); } } echo "PRINTING TABLE....<BR>"; echo "<table border=Ƈ'>"; for ($i = 1; $i <= 1000; $i++) { echo "<TR><TD>[$i]"; for($j = 1; $j <= 20; $j++) { echo "<TD>". $data[$i][$j]; } echo "<BR>"; } echo "</table>"; ?>
View Replies !
View Related
Loops, Php And Mysql Data
I am writing a profile creator script where a user gets a URL invite in their mail in the form of; http://domain.com/profile-create.ph...kd98jadf098asdf Things are working well except for a small annoyance in which someone might have a solution to. In the event that someone accesses profile-create.php without an access_code the script generates a warning. If there is an access_code that matches the access code entered in the MySQL database then they are directed to the profile creation page. My problem is; If someone accesses profile-create.php with an access_code but it doesn't match any entries in the database I would like to generate a warning. When I try to do this with an else statement it produces an error for every access code listed in the database which isn't the correct one, so I could end up with a successful profile creation page with a bunch of errors. I've played with break and pattern matching but no results. Here is the script below. [color=blue] >---------------------------------------------------------------<[/color] <?php $access_code = $_GET['access_code']; if ($access_code) { echo db_connect(); $result = mysql_query('SELECT random_link FROM invites'); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $random_link_db = $row[0]; if ($random_link_db == $access_code) { echo profile_creator_page(); } // Would like to insert an error warning here } } else { echo "Error!" } function profile_creator_page() { echo "All's well!"; } ?> [color=blue] >---------------------------------------------------------------<[/color]
View Replies !
View Related
Struggling With Nested While Loops
I am trying to execute 2 queries and then output the results in 2 loops but maybe I'm using the wrong approach. $query_1 = "SELECT field_1 FROM table_1"; $result_1 = mysql_query($query_1) or die(mysql_error()); $query_2 = "SELECT field_2 FROM table_2"; $result_2 = mysql_query($query_2) or die(mysql_error()); while ($row_1 = mysql_fetch_row($result_1)) { while ($row_2 = mysql_fetch_row($result_2)) { print $row_1[0] . ' - ' . $row_2[0] . '<BR>' } }
View Replies !
View Related
While Loops With SUBSTRING_INDEX Problem
From the field 'division', I'm trying to take the number to the left and divide it by the number to the right of a "/" and then add 1 each time a condition is met. So that 10/1 = 11 and 5/2 = 3.5 whenever field_id=9 etc. The code below (when without the while loops) works for a single row. But I'm new to PHP and cannot get it to loop correctly without outputing the right number against every left number in the division field, so 6 rows outputs 36 results instead of just 6 results. Code:
View Replies !
View Related
Loops / Page Numbers
What I'm trying to do, is look through a directory, grab all the files, store it into a variable, then divide the total number of files by 25 (the amount I want on each page) then echo it onto each page.I have everything set up thus far, however, I seem to be only able to get the 1st page to show the 1-25 and each page thereafter shows up blank. I think the problem is where I callif ($_GET["page"]==$single_page).I've tried changing that to WHILE but it ends up looping till I get a 30 second timeout.Any ideas or suggestions?
View Replies !
View Related
Associative Arrays And Loops
I am trying to make database calls on what will potentially be a very large database.I would like to receive query results from the database using mysql_fetch_array() to get an associative array. Once I have this array, I want to show certain fields using my own formatting. If there are no fields, I want to show a "No Results" message. My code thusfar looks like this, and it does nothing if there is an invalid database query: Code:
View Replies !
View Related
Eliminating Multiple Do-while Loops
I've been using the foll type of coding since i started php a while ago but i know multiple do-while loops can be eliminated to make the code more efficient. i've tried various combinations but i cannot seem to make it work. Would anyone be able to have a look and tell me how i can change it? Code:
View Replies !
View Related
|