Ok, so here goes.
I need to retrieve data, (ie. id, title, introtext.) from my database and store each in a variable.
I then need to repeat this for the next 4 records.
So I would have 5 sets of variables.
I then want to output each set of variables in a table (for now)
I have managed to retrive and store the data for the first row, but don't know where to start with repeating the task for the next 4 records.
Here is my code, if you don't have enough info jut ask.
Thanks. -
-
<?php
-
-
connecttodb($servername,$dbname,$dbusername,$dbpassword);
-
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
-
-
{
-
global $link;
-
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
-
if(!$link){die("Could not connect to MySQL");}
-
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
-
}
-
-
-
$results = @mysql_query("SELECT * FROM jos2_content WHERE `state` =1 AND `ordering` !=0 ORDER BY ordering ASC LIMIT 0,5");
-
-
-
while ($result = mysql_fetch_array($results)) {
-
$id = $result['id'];
-
$title = $result['title'];
-
$introtext = $result['introtext'];
-
}
-
-
?>
-
-
-
<FORM>
-
-
<B>Output:</B><BR>
-
-
<table>
-
<tr><td><?php echo $id ;?></td></tr>
-
<tr><td><?php echo $title ;?></td></tr>
-
<tr><td><?php echo $introtext ;?></td></tr>
-
</table>
-
-
5 2730
This is how I normally do it. - <?php
-
if (count($results))
-
{
-
$i = 0;
-
while ($i < count($results))
-
// or if you only wanted to return a total of 5 records
-
// while ($i < 5)
-
{
-
$retarray[$i]["id"] = $result[$i]["id"];
-
$retarray[$i]["title"] = $result[$i]["title"];
-
$retarray[$i]["introtext"] = $result[$i]["introtext"];
-
++$i;
-
}
-
}
-
?>
-
-
<form>
-
<b>Output:
-
-
-
<table>
-
<?php
-
$i = 0;
-
while ($i < count($retarray)){
-
print "<tr><td>".$retarray[$i]["id"]."</td></tr>";
-
print "<tr><td>".$retarray[$i]["title"]."</td></tr>";
-
print "<tr><td>".$retarray[$i]["introtext"]."</td></tr>";
-
++$i;
-
}?>
-
</table>
Thanks for your reply. I have amended my code and now when run the only output I have is 'output'.
Here is the Source code of the output.
[html]
<html>
<head>
</head>
<body>
<b>Output: </b>
<table>
<tr><td></td></tr><tr><td></td></tr><tr><td></td></tr> </table>
</body>
</html>
[/html]
Can you see the problem with my code?
Thanks.
Thanks for your reply. I have amended my code and now when run the only output I have is 'output'.
Here is the Source code of the output.
[html]
<html>
<head>
</head>
<body>
<b>Output: </b>
<table>
<tr><td></td></tr><tr><td></td></tr><tr><td></td></tr> </table>
</body>
</html>
[/html]
Can you see the problem with my code?
Thanks. -
-
-
<html>
-
<head>
-
</head>
-
-
<body>
-
<b>Output: </b>
-
-
-
<table>
-
<tr><td></td></tr><tr><td></td></tr><tr><td></td></tr> </table>
-
-
</body>
-
</html>
-
Sorry, typo on my part. Should have been results, not result. - {
-
$retarray[$i]["id"] = $results[$i]["id"];
-
$retarray[$i]["title"] = $results[$i]["title"];
-
$retarray[$i]["introtext"] = $results[$i]["introtext"];
-
++$i;
-
}
Thanks, I've tried that but I'm still getting the same output, see bottom of post. -
-
<?
-
-
$results=mysql_query("SELECT id, title, introtext FROM jos2_content WHERE `state` =1 AND `ordering` !=0 ORDER BY ordering ASC");
-
-
-
if (count($results))
-
{
-
$i = 0;
-
while ($i < count($results))
-
// or if you only wanted to return a total of 5 records
-
// while ($i < 5)
-
{
-
$retarray[$i]["id"] = $results[$i]["id"];
-
$retarray[$i]["title"] = $results[$i]["title"];
-
$retarray[$i]["introtext"] = $results[$i]["introtext"];
-
++$i;
-
}
-
}
-
?>
-
-
<html>
-
<head>
-
</head>
-
-
<body>
-
<b>Output: </b>
-
-
-
<table>
-
<?php
-
$i = 0;
-
while ($i < count($retarray)){
-
print "<tr><td>".$retarray[$i]["id"]."</td></tr>";
-
print "<tr><td>".$retarray[$i]["title"]."</td></tr>";
-
print "<tr><td>".$retarray[$i]["introtext"]."</td></tr>";
-
++$i;
-
}?>
-
</table>
-
-
</body>
-
</html>
-
-
[html]
<html>
<head>
</head>
<body>
<b>Output: </b>
<table>
<tr><td></td></tr><tr><td></td></tr><tr><td></td></tr> </table>
</body>
</html>
[/html]
Thanks, I've tried that but I'm still getting the same output, see bottom of post. -
-
<?
-
-
$results=mysql_query("SELECT id, title, introtext FROM jos2_content WHERE `state` =1 AND `ordering` !=0 ORDER BY ordering ASC");
-
-
-
if (count($results))
-
{
-
$i = 0;
-
while ($i < count($results))
-
// or if you only wanted to return a total of 5 records
-
// while ($i < 5)
-
{
-
$retarray[$i]["id"] = $results[$i]["id"];
-
$retarray[$i]["title"] = $results[$i]["title"];
-
$retarray[$i]["introtext"] = $results[$i]["introtext"];
-
++$i;
-
}
-
}
-
?>
-
-
<html>
-
<head>
-
</head>
-
-
<body>
-
<b>Output: </b>
-
-
-
<table>
-
<?php
-
$i = 0;
-
while ($i < count($retarray)){
-
print "<tr><td>".$retarray[$i]["id"]."</td></tr>";
-
print "<tr><td>".$retarray[$i]["title"]."</td></tr>";
-
print "<tr><td>".$retarray[$i]["introtext"]."</td></tr>";
-
++$i;
-
}?>
-
</table>
-
-
</body>
-
</html>
-
-
[html]
<html>
<head>
</head>
<body>
<b>Output: </b>
<table>
<tr><td></td></tr><tr><td></td></tr><tr><td></td></tr> </table>
</body>
</html>
[/html]
Sorry, not really sure where my mind is today. I always use mdb2 or previously db from PEAR. I think it wasn't fetching the fieldnames.
I hope this works for you. - $i = 0;
-
while $result = mysql_fetch_array($results)){
-
$retval[$i]["id"] = $result["id"];
-
$retval[$i]["title"] = $result["title"];
-
$retval[$i]["introtext"] = $result["introtext"];
-
++$i;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Terry |
last post by:
I have a number of input boxes used to display totals based on
selected items for each row in a table. There are more than a few
rows that are identical, except for the form field name. I have...
|
by: Toby Austin |
last post by:
I'm trying to replace <table>s with <div>s as much as possible.
However, I can't figure out how to do the following…
<table>
<tr>
<td valign="top" width="100%">some data that will...
|
by: news.cablevision.qc.ca |
last post by:
Hi!
I want, using a form, enter a number and then, read that number of records
in a database and store information in variables. The only way I see to do
this is to do a FOR...NEXT to the...
|
by: Megan |
last post by:
Hi everybody-
I have 2 tables, Hearings and Rulings. Both have primary keys called,
CaseID, that are autonumbers. I don't want both tables to have the
same autonumber. For example, if Hearings...
|
by: Wilson Wu |
last post by:
Hi,
How can I find the field value in the dataset and then assign one of the
field value to a variable ?
Thanks
Wilson
|
by: phil-news-nospam |
last post by:
Is there really any advantage to using DIV elements with float style
properies, vs. the old method of TABLE and TR and TD?
I'm finding that by using DIV, it still involves the same number of...
|
by: Muzzy |
last post by:
Hi,
I've used information on these newsgroups to build many pages. So I thought that now that I have my script working (something that I've been working on for about a week), I should post it so...
|
by: MissMarie |
last post by:
I've been playing around with DIV tables in myspace to better learn how to rewrite my own code for my business site without having to pay someone to design it. I've tried embedding a slideshow into...
|
by: Birky |
last post by:
Assign the output of a select to a variable?
I am unable to find a way to assign the results of an SQL statement to a variable. I know how to assign the SQL statement to a variable but again no...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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: 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,...
| | |