Basically, I need a way of getting around a problem I recently found out...
When I don't have a sequential row ID pattern (1,2,3,4), which comes up like (2,5,6), the script will return absolutely nothing.
This is my current PHP code:
[PHP]<?php
$sql_queries[1] = "SELECT id FROM `news`";
$sql_query = mysql_query( $sql_queries[1] ) or die();
$sql_num_rows = mysql_num_rows( $sql_query ) or die();
for ( $i = $sql_num_rows; $i > 0; $i-- )
{
$sql_queries[2] = "SELECT type,date,headline FROM `news` WHERE id = '".$i."'";
$sql_query2 = mysql_query( $sql_queries[2] ) or die();
$sql_array = mysql_fetch_array( $sql_query2 ) or die();
echo "<tr>\n";
echo "<td>".$sql_array['date']."</td>\n";
echo "<td>".$sql_array['headline']."</td>\n";
echo "<td>".$sql_array['type']."</td>\n";
echo "<td align=\"center\"><a href=\"index.php?page=news&act=edit&id=".$i."\"><i mg src=\"images/form_edit.png\" alt=\"Edit\"></a></td>\n";
echo "<td align=\"center\"><a href=\"index.php?submit=true&page=news&act=list&id =".$i."\"><img src=\"images/form_delete.png\" alt=\"Delete\"></a></td>\n";
echo "</tr>\n";
}
?>[/PHP]
So I basically need a workaround for listing a table that will show all "news" rows, even if the IDs weren't in a specific sequence (eg; I deleted a few articles, added and modified some, etc).
9 11298
For a Small work You have code lots of Thing. ;)
Please Try to change my Coding as suite to Your Requirement.
Here in my Coding I am using a Another Table Named Products.And from that I am going to fetch all the values. And in <TD> i am going to Display product ID again as the out put. Along with that to your edit And delete pages, again the same ID will Pass. Since it is in side the while loop, I can Print it for Any Number of Rows in the table.
[PHP]<?php
require('dbcon.php');
$sql = "SELECT * FROM products"; // change This Line
$result = mysql_query($sql) or die();
while( $row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['p_id']."</td>\n";//change the column Names
echo "<td align=\"center\"><a href=\"index.php?page=news&act=edit&id=".$row['p_id']."\"><img src=\"images/form_edit.png\" alt=\"Edit\"></a></td>\n";
echo "<td align=\"center\"><a href=\"index.php?submit=true&page=news&act=list&id =".$row['p_id']."\"><img src=\"images/form_delete.png\" alt=\"Delete\"></a></td>\n";
echo "</tr><br>";
}
?> [/PHP]
For a Small work You have code lots of Thing. ;)
Please Try to change my Coding as suite to Your Requirement.
Here in my Coding I am using a Another Table Named Products.And from that I am going to fetch all the values. And in <TD> i am going to Display product ID again as the out put. Along with that to your edit And delete pages, again the same ID will Pass. Since it is in side the while loop, I can Print it for Any Number of Rows in the table.
[PHP]<?php
require('dbcon.php');
$sql = "SELECT * FROM products"; // change This Line
$result = mysql_query($sql) or die();
while( $row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['p_id']."</td>\n";//change the column Names
echo "<td align=\"center\"><a href=\"index.php?page=news&act=edit&id=".$row['p_id']."\"><img src=\"images/form_edit.png\" alt=\"Edit\"></a></td>\n";
echo "<td align=\"center\"><a href=\"index.php?submit=true&page=news&act=list&id =".$row['p_id']."\"><img src=\"images/form_delete.png\" alt=\"Delete\"></a></td>\n";
echo "</tr><br>";
}
?> [/PHP]
I noticed that in your script you used "while( $row = mysql_fetch_assoc($result))". What exactly does this do? I found this in a few tutorials but none of them seemed to have explained what it serves in the script.
Thanks for your help though, I'll try it out. :)
you are welcome any time. :) Click here to find out the more about this function.
Example: - while (1 == 1) {
-
// code
-
}
-
The 'while' loop executes as long as the condition between the parentheses is true. Since in this example the expression is aways TRUE, the while will run forever.
In your post you have a condition '$row = mysql_fetch_array($result)'. mysql_fetch_array is a function that returns either a row or FALSE (when there are no more rows).
So your statement - $row = mysql_fetch_array($result)
says:
Execute the code within the while loop as long as the result of the condition ($row=...) is true, i.e. as long as there are rows to retrieve. When there are no more rows the while loop will end.
Hope this is clear.
Ronald :cool:
Example: - while (1 == 1) {
-
// code
-
}
-
The 'while' loop executes as long as the condition between the parentheses is true. Since in this example the expression is aways TRUE, the while will run forever.
In your post you have a condition '$row = mysql_fetch_array($result)'. mysql_fetch_array is a function that returns either a row or FALSE (when there are no more rows).
So your statement - $row = mysql_fetch_array($result)
says:
Execute the code within the while loop as long as the result of the condition ($row=...) is true, i.e. as long as there are rows to retrieve. When there are no more rows the while loop will end.
Hope this is clear.
Ronald :cool:
Ahh, thanks!
Thank you both for your help. The function does work and I've implemented it in all of the areas I needed to, and they all function excellent. This couldn't have been anymore quicker, I'm quite impressed.
Is there a way to reverse this function? Such as "id" 10-1, instead of 1-10?
from SQL script itself you can make it. - Select * from news GROUP BY id ORDER BY id asc
for Ascending order you can use ASC
for Descending order you can use DSC
Ahh, thanks!
Thank you both for your help. The function does work and I've implemented it in all of the areas I needed to, and they all function excellent. This couldn't have been anymore quicker, I'm quite impressed.
Is there a way to reverse this function? Such as "id" 10-1, instead of 1-10?
Yes, when you want to MySQL select only the rows with column 'id' 1-10 in reverse order, do - SELECT * FROM table WHERE id>=1 AND id<=10 ORDER BY id DESC;
Ronald :cool:
Of course, I was thinking too much on the PHP side, I forgot all about the SQL order by.
Thanks again. :)
You are welcome! See you next time.
Ronald :cool:
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paris_Sucks |
last post by:
I'm trying to redirect when testing for certain condidtions as shown
below. When the conditions are ture, it redirects, but still goes ahead and
processes the sql query. What am I doing wrong??? ...
|
by: Richard M |
last post by:
Can anyone please lend me a hand,
I'm trying to design a cookie based password system.
I have managed to do everything bar one thing that is driving me
crazy.
I have a MYSQL database called...
|
by: Quintin |
last post by:
Hi ,
I'm trying to make an app in vc++ that access my MySQL db and then
email me some data. I'm mailing with winsock. Whenever i email with
winsock then i can't connect with MySQL db(anymore)....
|
by: PHP_Paul |
last post by:
Ok, I'm trying to poineer into the wonderful area of PHP/MySQL programming, but I'm having some difficulties. http://www.paulhq.com/php/freepage.html should register, but when anyone fills something...
|
by: automation |
last post by:
There is a truncation error of my Web Application using PHP 5.04, MySQL 5.022, and HTML(IE 6.0) whereby the MySQL Result Set is being truncated on the HTML page, even though the CSS Div Page Height...
|
by: Atli |
last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t.
In situations where your PHP application...
|
by: JPhilS |
last post by:
Hi to all Webmaster!
I have discover this problem when I inserted the scores of the students i a centain subject.
I am making a school project with regards to saving students' record.
first, I...
|
by: shauna00 |
last post by:
Hi guys,
I want to basically take in a username and password and then check to see if its in the mySQL database. The problem is though that I can print the variables yet when I use then in the php...
|
by: bilibytes |
last post by:
hi,
i tried to create sort of a db abstraction layer in order to be able to change of database (Mysql | oracle | pg |...).
i implemented that on a obejct oriented way.
i made a sql factory in...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |