How Do Iterate Through MySQL Records And Displat Them?
Basically I have a page where a person can choose from a list of stories posted by people, or they can choose to submit their own. I've managed to do the page where they can submit their on. The way i did this was to set up a mySQL database, and table in the database called stories. I then made an id (primary key), author, title, body and date field in the table. When the user submits their story its given an id (which increments on each submition) and their author, title and body fileds are sent to the database. The date key is automatically stamped in.
Ive also managed to have the submition page display what they submitted by using the count(*) function, their story would be the last to be submited , so it pulls that from the database and displays it.
Now what I want to do - on the page where they can pick a story, is have the page connect to the database and grab each record and display the author and title on the page next to a radio button. And have the id for the story in a hidden field or variable so when the user to chooses a particlar story it uses the id to grab the story from the database.
So how do iterate through all the records in the database, and display each one?
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
How To Iterate Through Array?
I have a file that contains the output of time(). A different time is on each line of the file, and each line represents a visit to the website. I want to calculate the total visits per day, month and year. So, I dump the file into an array, and then iterate through the array testing each element and incrementing a counter for each respective time period. But what is the best way to iterate through the array? The foreach($av) does not seem to be working. $viscounter = 'viscounter' // this is all that's in the file (will be hundreds of lines): 1089828547 1089828548 1089828549 1089828550 $av = file($viscounter); $v24h = 0; $v30d = 0; $v365d = 0; foreach($av) // ? { if($av>time()-(3600*24)) { ++$v24h; } if($av>time()-(3600*24*30)) { ++$v30d; } if($av>time()-(3600*24*365)) { ++$v365d; } } echo "<br>v24h=".$v24h; echo "<br>v30d=".$v30d; echo "<br>v365d=".$v365d;
Iterate Through Multidemensional Array
I'd like to be able to iterate through the following array w/o having to hardcode the number of times it does so. As you can see by the commented out lines, I've tried using a variable set using the 'count' function, but get only partial results when I do so. I've tried every combination that I can think of to do this, but nothing has worked.
Iterate From Middle Of Array
I want to iterate through an array starting at a known index. However the indexes are not linear. For example I have an array of events keyed by timestamp. $eventList = array (1064263264 => "event1", 10642635555 => "event2", 1064266666 => "event3", 1064267782 => "event4", 1064268812 => "event5"); I basically want to do a "for" or "foreach" but I don't necessarily want to start at key 1064263264 (event1). I'm looking for something Like $startEvent = 10642635555; $endEvent = 1064267782; for ($event = $startEvent; $event < $lastEvent; $event = nextKey()) { print $eventList[$event]; } To make it worse, I can't use a foreach and simply "if" my way out of it because I am already iterating over the entire list and I would like to avoid a O(x^2) algorithm. foreach ($eventList as $currentEvent) { ..... // processing $startEvent = 10642635555; $endEvent = 1064267782; for ($event = $startEvent; $event < $lastEvent; $event = nextKey()) { print $eventList[$event]; } ..... // more processing } Of course there is no "nextKey()" and before rolling my own I thought I would ask if PHP already has a solution for this that I've missed.
How To Iterate Throut An Array?
I have an array like this: Array ( [0] =Array ( [1] =Array ( [ID] =7A585DC0 [Co] =Array ( [C] =nat [b] =7A585DC0 ) [T] =Array ( [1] =Array ( [ID] =>7A585DC0 I want to delete some items from it. I think I have to do it recursively, but PHP says: "Fatal error: Only variables can be passed by reference in c:wwwp.php on line 51" The function looks like this: function Del(&$tresc) { if (is_array($tresc['T'])) Del($tresc['T']); else if (!$tresc['T']) { for ($x=0;$x<count($tresc);$x++) { Del($tresc[$x]); //THIS IS LINE 51 } } else if (($tresc['Co']['C']=='hl') and (!$tresc['Co']['n'])) array_splice($tresc,0,1); } How to iterate through all elements and delete some of them?
PHP/MySQL Records Per Row
Is there any way to have the records pulled from a mysql database display like this in a table: record1 | record2 record3 | record4 etc: I've been thinking about this for about three months and have yet to come to a good solution.
How Many Records Can MYSQL 4.1.14 NT Via TCP/IP Can Hold?
how many records can MYSQL 4.1.14 NT via TCP/IP can hold? how many records per table can MYSQL 4.1.14 NT via TCP/IP can hold? how many records in a table can MYSQL 4.1.14 NT via TCP/IP can hold given the table has a field Primary key - BIGINT(20) autoincrement? need your opinion..Here are the facts: MY system is an internal one. My Server--Is only a Desktop.. Celeron 2.8 Gigahertz, 40Gig Baracuda HD, 512 MB memory.. And it serves an average of 20 users a day and it also serves me for my system development..The average records inserted per month on mYSQL is 10,000 records..We foresee that system would be used for about 10 years.
Counting Records In MySQL
I've been trying to write a page that checks the database and counts the number of records and if the number is less than 3000, it loads page X, but if the number is equal to 3000, it loads page Y. I know this should be a simple if/else statement but it's the checking and counting that I cannot get to work.
Problems Updating Records In MySQL With PHP
Sorry to ask what is probably a simple answer, but I am having problems updating a table/database from a PHP/ PHTML file. I can Read From the Table, I can Insert into Table/Database, But not update. Here is a copy of the script I am using. I do not know what version of MySQL my host is running nor do I have Shell Access to it. I would like to setup a script so my website users can update thier records without my intervention. Also you may send replies to stevennyoung@yahoo.com Begin Script <?php $db = mysql_connect("localhost", "username", "password"); mysql_select_db("databasename",$db); if ($id) { if ($submit) { $sql = "UPDATE tablename SET EMail='$EMail',Username='$Username',Password='$Pas sword',Item= '$Item',Price='$Price',URL='$URL' WHERE Username='$Username'"; $result = mysql_query($sql); echo "table Updated."; } else { // query the DB $sql = "SELECT * FROM tablename WHERE Username='$Username'"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> <form method="post" action="<?php echo $PHP_SELF?>"> e-Mail Address:<input type="Text" name="EMail" value="<?php echo $myrow["EMail"] ?>"><br> Username:<input type="Text" name="Username" value="<?php echo $myrow["Username"] ?>"><br> Password:<input type="Text" name="Password" value="<?php echo $myrow["Password"] ?>"><br> Item:<input type="Text" name="Item" value="<?php echo $myrow["Item"] ?>"><br> Price:<input type="Text" name="Price" value="<?php echo $myrow["Price"] ?>"><br> Item URL:<input type="Text" name="URL" value="<?php echo $myrow["URL"] ?>"><br> <input type="Submit" name="Update" value="Update table"> </form> <?php } } else { // display list of articles $result = mysql_query("SELECT * FROM tablename",$db); while ($myrow = mysql_fetch_array($result)) { printf("<a href="%s?Username=%s">%s %s</a><br> ", $PHP_SELF, $myrow["Username"], $myrow["Item"], $myrow ["URL"]); } } // Close DB Connection mysql_close() ?>
Grabbing Specific MySQL Records
I have set up a target page to pull information out of a mySQL database to populate the page. The problem is that it is only pulling from the first record and not the record specified. What do I have to do to pull from a specific record? I am using Dreamweaver MX to administer the site on WinXP using PHP if that information will help.
PHP Mysql Outputting Records Repeatedly.
I have not messed with php/mysql in a while and have come up with a problem I have run up against before but cannot remember the solution. See the following page:
Very Slow MySQL SELECT Query After 11 Records
I have a very weird problem I moved my database to new IIS 6.0 + PHP 4.3 + MySQL 4.0 setup. I tried to check with myphpadmin if everything works fine, but I couldn't browse my table (125 records). I used SELECT option and found out that SELECT query up to 11 records is ok, but after that, even if I choose to limit query to 12 records, it just hangs. I tried selecting from the beginning of the table and from the middle - same thing, but when I run query localy on mysql console, everything works fine
How Do I Prevent Mysql From Printing Redundant Records?
I have a very urgent question. I am using php and mysql. Say I have a database full of names and email addresses. I want to do a query that when you type in the name or partial name, mysql will only show a name once, even if it is in the database 200 times. Then I want to click on that name and have mysql show the 200 email addresses associated with that name (which I know how to do). See below:
Inserting Master-detail Records In Mysql
how I can insert a master-detail record in mysql? I use an auto-increment key, but I don't know how I can find that key for inserting the detail records, I tried to make a 'highest-key' function with 'select MAX(field)...' but in php it gives the wrong key (in mysql it gives the right one).
Changing Data In Objectified Mysql Records
I am using this very slightly modified function found here: http://us2.php.net/mysql_fetch_object to make mySQL rows into objects of type $classname: // This takes db result rows and makes real objects of $classname type function &buildObj($result, $classname) { while($row = mysql_fetch_assoc($result)) { if ($row === null) return null; /* Create the object */ $obj =& new $classname; /* Explode the array and set the objects's instance data */ foreach($row as $key => $value) { $obj->{$key} = $value; } $objs[] = $obj; } return $objs; } This is called by a retrieve: function: function retrieve($where) { echo $query = "SELECT * FROM $this->table WHERE " . join(' AND ', $where); $result = mysql_query($query); $rows =& buildObj($result, get_class($this)); mysql_free_result($result); return $rows; } The problem I am encountering is that if I do this in my script: $listing = new Listing; $listings = $listing->retrieve(array('row > 1') ); foreach ($listing as $aListing) { $aListing->address='xxxxxxxxxxxxx' } foreach ($listing as $aListing) { print_r($aListing); } The value in $aListing->address is unchanged. However if I do this: $listing = new Listing; $listings = $listing->retrieve(array('row > 1') ); foreach ($listing as $aListing) { $aListing->address='xxxxxxxxxxxxx' $newArray[] = $aListing; } foreach ($newArray as $aListing) { print_r($aListing); } I see the update values in $aListing->address .
Break MySQL Records Into CSS Columns Automatically
I'm making a page of links, each assigned a category. There are two tables - "favourites_links" and "favourites_categories". This is how the links currently appear: Category 1 Name Link 1 Link 2 Link 3 Category 2 Name Link 1 Link 2 Link 3 What I want to do is break my page into columns. Basically, set a number of columns (e.g. 3), count the total records, and work out how many records to show per column. Each column should be enclosed in a <div class="column"> wrapper, which floats to the left. Code:
Having Trouble Looping Through Records In A MySQL Table
I'm having some trouble looping through the records of a MySQL table using PEAR DB. Here is my code: $navigationSql = "SELECT * FROM nav"; $navigation =& $db->query($navigationSql); while ($navigation->fetchInto($nav)){ define("NAVIGATION", "<a href="$nav[2]" target="$nav[3]" title="$nav[1]">$nav[1]</a><br />"); } The when I enter: print NAVIGATION; into any of my PHP scripts it should loop through the stuff in table named nav, but it is only displaying the first record and so far everything I've tried does not seem to make it loop through all of the records in the table and I can't figure out why not.
Displaying Priorty Records And Normal Records In The One Dataset.
I have a page which displays the records from the database and uses paging to display the records in groupes of 10 per page. I now want to be able to show results depending on the database field (display) in the database (can be set to either "full" or "limited"). If the record and the field display is equal to "full" i want to then display this record at the top of the record set and display all the fields. If the record is equal to "limited" i want to display different results and they must show after all the records which are equal to "full". I can't output two different recordsets as i use paging and only want to show 10 records per page. Code:
How To Control Access To Specific Mysql Records For A User
I am just trying out a php form, when posted submits data to database. The system is we allow everyone to submit the form. As administrator I can view and edit the records. But whoever submits the form should be a registered user and he should be able to only view his records but not edit.
Can I Convert MySQL Db Records Into Microsoft Word Documents?
i found the solution to export file from mysql db into *.csv. but is there anyway to convert the contents into *.doc and save in my webserver and providing a link for the end users to download the word file? FYI, the database records are obtained by end users submitting the forms themselve and i saved it in my db...
MySQL Inserting Into Multiple Tables Linked Records
Using MySQL can anyone tell me how to most efficiently insert a collumn in 'table1' retrieve the id from this record and insert mutliple records into 'table2' with a collumn for the id in 'table1' which links the two recordsets
How To Access And Modify And Add Records In MySQL Table From A Windows Client?
The MySQL table is located in a Linux Server OS, so I need the information from that table for updating Paradox tables in Windows OS. If a PHP program in the Linux Server can save a file with the information I need, can I make a PHP program in Windows OS to get the saved file or even better, to access the table in the Linus Server OS?...
Count,sort,diplay Number Of Records In MySQL Table
I am designing a search engine using PHP/MySQL and I am logging all search terms into a MySQL table. I was wondering how I count the number of occurences of a term in the table, and then sort and display the terms in descending order. For example, if the column in my table with search terms looked something like this: clothes casinos clothes homes homes clothes How can I count and order the records so that I can display according to how many times they appear, like so: 1. clothes(3 entries) 2. home(2 entries) 3. casinos (1 entry)
Need To Update Multiple Mysql Records With A Single Form Submit
I have a wish list like a shoping caty based on a mysql databse where i retrieve records with php I need to update the quantity which is in a text fields : input type = text name = quantity[] value = $quantity input type = checkbor value = $id name = id[] I need to update quantities based on the primary id represented with id with a single submit.
Reading An Array Of Records From MYSQL Then "Save As" Them Back As Copy
Hi all, Ok here is what I want to do then I will show you what I have. I have some records in a MySQL DB that I want to store in an array and then immediately copy them back where the only thing that would change would be one field - the date. So here is my table: ------------------------------------- |userName | date | value | -------------------------------------- | theDano | 200005 | 123 | -------------------------------------- | theDano | 200005 | 345 | -------------------------------------- | theDano | 200004 | 678 | -------------------------------------- | joeUser | 199908 | 897 | --------------------------------------- So if I am querying for the user "theDano" on a specific date (format yyyymm) in this case 200005 (year 2000 month of May) Here is my array to query all records to fullfill this request: if ($update=="future") { if ($month!="01") { $db = mysql_connect("localhost", "dbuser"); if($db) { mysql_select_db("dbName",$db); $result = mysql_query("SELECT * FROM tableName WHERE userName = '$userName' AND date = '$date' ORDER BY value DESC ",$db); if ($result) { $num=mysql_numrows($result); $i=0; while($i < $num) { $userName = mysql_result($result,$i,"userName"); $value = mysql_result($result,$i,"value"); echo "<tr>"; echo "<td width=Ê×'>" . $userName . "</td>"; echo "<td width=ï`'><div align='right'>" . $value . "</div></td>"; echo "</tr>"; ++$i; } } } } } Now I want to take the array of records queried which was: | theDano | 200005 | 123 | -------------------------------------- | theDano | 200005 | 345 | and copy them to look like this back into the database: | theDano | 200006 | 123 | -------------------------------------- | theDano | 200006 | 345 | so you can see that the only thing changed was increasing the month by one. Would seem simple to most but I am having a tough time wirting the code to read the array and increase each date value by "1". Thank you in advance for anyone that replies. ) Dano
DNS Records
Hey everyone, I'm trying to get a DNS record of an IP using PHP's checkdnsrr() and getmxrr(). I used echo checkdnsrr($REMOTE_ADDR); and echo getmxrr($REMOTE_ADDR); but I receive an error that the PHP build does not support it, but I am using PHP 4.0.5 and in the manual, it shows that PHP 3 and 4 supports the two commands. Any help?
Next N Records
how do I create a "next n records" interface? I want to initially output a specific number of rows, and have a next/previous scenario where the user can click on either and scroll forward or back through the records. I'm currently outputting all rows through mysql_fetch_array: Code:
HOW TO GET HOW MANY RECORDS YOU HAVE IN DB
I finally discover the way how to get what I need. Let's look to my code: ----------------------- CODE ------------------------> $res = @mysql_query("SELECT COUNT(*) AS ToT FROM table_name"); $dato = mysql_fetch_array($res); echo($dato[ToT]); ----------------------- CODE ------------------------> With this code you will get how much records do you have in DB. PS: Whan you think that everything is over and that you are at bottom of trying push harder to see what is under.
Last 5 Records
is there an easy way to grab the last 5 distinct records from an unindexed database? (php/mysql)
Number Of Records
I want to find out how many records, but I am using LIMIT in mySQL so if set the limit to 20 and I have 56 records then it will display 20 instead of 56. I have been using: mysql_num_rows What should I be using?
What To Do If There Are No Records In The Database
I am pulling data from the database and displaying it using the code below: $db = mysql_connect("localhost", "root"); mysql_select_db("pyramid",$db); $result = mysql_query("SELECT * FROM table where area='Wales'",$db); while ($myrow = mysql_fetch_row($result)) { echo "blah blah"; } ?> What do I do if there are no records? At the moment it just doesn't display anything. How do I get it to display a message saying there are no records?
Updating Records
I am know to PHP i've been tring to do this **** for days now could someone PLEASE help me. Ok Im tring make an admin page where user can update multiple records at same time using checkboxes.Im using email as primary key. Here is the code **form page** <form action="update.php" method="post"> <input type="submit" name="submit" value="Select First 10 Names"> </form> **update.php** <?php if ($submit =="Select First 10 Names") { ?> <form method="post" action="<?php echo $PHP_SELF; ?>"> <hr size=3> <?php $query = mysql_query("select * from Names order by Last_Name limit 10"); while($row = mysql_fetch_array($query)) { $first_name= $row["First_Name"]; $middle_name = $row["Middle_Name"]; $last_name = $row["Last_Name"]; $email = $row["Email"]; $photo=$row["Photo"]; ?> <table width="100%" border="0" > <tr> <td align=right width=50%> <?php echo "First Name: <input type=text name="first_name" size="30" maxlength="40"value="$first_name" ><br> "; echo "Middle Name: <input type=text name="middle_name" size="30" maxlength="40" value="$middle_name"><br> "; echo "Last Name: <input type=text name="last_name" size="30" maxlength="40" value="$last_name"><br><br> "; echo "Email: <input type=text name="email" size="30" maxlength="40" value="$email" readonly><br><br> "; echo "Photo: <input type=text name="photo" size="30" maxlength="70" value="$photo"><br><br> "; echo "<input type="checkbox" name="email[]" value="$email"><b>Update?</b> "; ?> </td> <td align=right width=50%> <?php echo "$photo"; ?> </td> </tr> </table> <hr size=3><br> <?php } ?> <input type=submit name=submit value="Update Enteries"> <input type=reset> </form> <?php } if ($submit=="Update Enteries") { if (is_array($email)) { while (list($key,$value) = each($email)) { $query= mysql_query ("update Names set First_Name = '$first_name' Middle_Name= '$middle_name' Last_Name='$last_name' where Email='$value'"); } } } ?>
Need Help With Deleting Records
I'm trying to add a link on the display of a record to delete that record. The main part of the record is stored in pw_event, then there are several rating fields for that record stored in pw_rate, and there is also several records for the first record stored in pw_whois. I need to have a link that will deleted everything for that record in all three tables. Here is my display script: PHP Code:
Retrieving Records With Between
I am trying to convert my asp website to php. So far so good, but I am having a problem with my SQL Statement that needs to us the Between. Here is what I am using: $sql = "Select * From tblTrack Where DateEntered Between $start and $end"; the vars start and end come from the same form. I don't have a problem when I hard code the dates, but when I use the variables, it won't retrieve anything.
Delete Records
I`m just trying to figure out the best method to delete my records and wondered if it was possible to do a DELETE statement to remove any records that don`t match. Here is my code so far: $Query="select blah its huge:-)"; $RESULT=mysql_query($Query); $my_rows=mysql_num_rows($RESULT); for ($a=0; $a<$my_rows; $a++){ mysql_data_seek($RESULT, $a); $Array = mysql_fetch_array($RESULT); printf("%s<BR>", $Array['Email']);} My Query returns Matched Records, what I would like to achieve is deletion of the records that didn`t match, would it be possible to do that under the printf statement. So delete all the records in the table that don`t match up to $Array['Email']??
Inserting Records
I'm trying to insert as many as records as possible but less than 100 into a table. First, i get the count inorder how many to add and then depending on the count, i get so many blank form fields to enter. Now, I noticed something really strange. 1.Only the last entered record is inserted into the table. 2.For example, when I type a name, Let us assume "WORLD", only w is inserted into the table and the rest is not.It is the same with all the form fields.whether, i'm trying to enter text or a number,only the first character is inserted.This sounds weird. Could somebody pls tell me why is this peculiar behaviour Here is the code:
Insert Records
When I try to insert a records in the database "socios", it doesn't inserted at all. Code:
Different Hyperlinks To Different Records
I'm displaying few records from a mysql table - pretty straight forward one. For each record i display on the form, i want to add a link to it.so, each link would display a different page(an html). PHP Code:
Displaying Some Records Once Only
I'm building a database via mysql/php which brings back records of events. My problem is that I only want to show the Venue for the event once and then list the events for that venue. ie Code:
Deleting Records
I've been trying to delete records since a week and i'm not able to do it. Before with this script, i was doing so many things at a time and i was confused.But, now, i just displaying the records from a table N trying to delete them.Would somebody pls guide me. I just wanna set the delete flag(i.e, i want to set the RBS_DELETE column to Y or something) when i check a check box to delete it. I've some 5 records displaying on my form and i want to check 3 of them and when i check them and hit submit, i want the RBS_DELETE column to have some value. PHP Code:
How Do You Sort Records?
I have a db table "person" that I can sort by title, first_name, last_name, or city with no problems at all because "title", "first_name", "last_name", and "city" are columns in the table. The requirement is to ALSO sort by department NAME. Problem is, I can't do that in the "person" table because the column is "department_id" which is a foreign key constraint to the department table. How do I sort the person records by department_name based on this? I am using PHP 4.3.2 and MySQL 4.0.10 Now, second problem and even more of a problem: The requirement is to ALSO sort by state NAME and country NAME. The fields "state" and "country" in the "person" table contain only an abbreviation of state and country; the corresponding full name of each state and country are found in respective XML files in /xml/state.xml and /xml/country.xml
Compare Records
is it possible to write a query to compare records in 2 tables, like if the number of records in the 2 tables are equal the query should return 1 if not equal the query should return 0. this can also be done in php like, get the number of records from one table and get number of records in another table, compare it and do the operation. but i wanted to know if its possible to do the compare records using the query itself. for example, if one table has 5 records, this should be compared with another table, if this table has 5 records, then the result of the query should be 1 else 0. if i use joints, it will return all the 5 records. so not sure how to make this in a query. anyone have any idea ?
Display Records
I want it to display all the records meeting the criteria specified in the script, but it only shows one records and should show about ten. any thoughts or pointers to get it to display more than one record. Code:
DESC Records
i want to make all the records be shown in a decending order, i've tried using: $sql = 'SELECT * FROM `repair` ORDER BY `repair` . `id` DESC';
Records Won't Delete
mysql_query("DELETE FROM guestbook WHERE id = '$recnum'"); It works fine on my local server but it doesn't delete records on the remote server. any ideas?
Count Records
How can I count how many records are in a table that match a news_id? I have this at the moment, but it doesn't seem to work. Code:
Retreiving All Records From Sql Db
IM trying to get cost values out of the db with a specific ID which is assigned to an item ID but i only get the last record in the database. $lala = mysql_query("SELECT `cost` FROM `table` WHERE `ID` = '1'"); while ($la = mysql_fetch_row($lala)){ $start = 0; $overall = $start+=$la[0]; } echo("$overall"); ...
Return Last 3 Records
I want to return the last 3 records added to my table called towns. sql = "select * from towns limit 3" This only returns the first three rows, I want the last 3. My primary key field is townID.
|