473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Assign table data to variable, and then repeat

23
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php 
  3.  
  4. connecttodb($servername,$dbname,$dbusername,$dbpassword);
  5. function connecttodb($servername,$dbname,$dbuser,$dbpassword)
  6.  
  7. {
  8. global $link;
  9. $link=mysql_connect ("$servername","$dbuser","$dbpassword");
  10. if(!$link){die("Could not connect to MySQL");}
  11. mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
  12. }
  13.  
  14.  
  15. $results = @mysql_query("SELECT * FROM jos2_content WHERE `state` =1 AND `ordering` !=0 ORDER BY ordering ASC LIMIT 0,5"); 
  16.  
  17.  
  18. while ($result = mysql_fetch_array($results)) { 
  19. $id = $result['id']; 
  20. $title = $result['title']; 
  21. $introtext = $result['introtext']; 
  22.  
  23. ?> 
  24.  
  25.  
  26. <FORM> 
  27.  
  28. <B>Output:</B><BR> 
  29.  
  30. <table>
  31.     <tr><td><?php echo $id ;?></td></tr>
  32.     <tr><td><?php echo $title ;?></td></tr>
  33.     <tr><td><?php echo $introtext ;?></td></tr>
  34. </table>
  35.  
  36.  
Jul 28 '07 #1
5 2730
ilearneditonline
130 Expert 100+
This is how I normally do it.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (count($results))
  3. {
  4.     $i = 0;
  5.     while ($i < count($results))
  6.     // or if you only wanted to return a total of 5 records
  7.     // while ($i < 5)
  8.     {
  9.         $retarray[$i]["id"] = $result[$i]["id"];
  10.         $retarray[$i]["title"] = $result[$i]["title"];
  11.         $retarray[$i]["introtext"] = $result[$i]["introtext"];
  12.         ++$i;
  13.     }
  14. }
  15. ?>
  16.  
  17. <form>
  18.     <b>Output: 
  19.  
  20.  
  21.     <table>
  22.     <?php 
  23.     $i = 0;
  24.     while ($i < count($retarray)){
  25.         print "<tr><td>".$retarray[$i]["id"]."</td></tr>";
  26.         print "<tr><td>".$retarray[$i]["title"]."</td></tr>";
  27.         print "<tr><td>".$retarray[$i]["introtext"]."</td></tr>";
  28.         ++$i;
  29.     }?>
  30.     </table>
Jul 28 '07 #2
keeps21
23
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.
Jul 28 '07 #3
ilearneditonline
130 Expert 100+
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <html>
  4. <head>
  5. </head>
  6.  
  7. <body>
  8.     <b>Output: </b>
  9.  
  10.  
  11.     <table>
  12.     <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>    </table>
  13.  
  14. </body>
  15. </html>
  16.  
Sorry, typo on my part. Should have been results, not result.

Expand|Select|Wrap|Line Numbers
  1. {
  2.         $retarray[$i]["id"] = $results[$i]["id"];
  3.         $retarray[$i]["title"] = $results[$i]["title"];
  4.         $retarray[$i]["introtext"] = $results[$i]["introtext"];
  5.         ++$i;
  6.     }
Jul 28 '07 #4
keeps21
23
Thanks, I've tried that but I'm still getting the same output, see bottom of post.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?
  3.  
  4.      $results=mysql_query("SELECT id, title, introtext FROM jos2_content WHERE `state` =1 AND `ordering` !=0 ORDER BY ordering ASC");
  5.  
  6.  
  7. if (count($results))
  8. {
  9.     $i = 0;
  10.     while ($i < count($results))
  11.     // or if you only wanted to return a total of 5 records
  12.     // while ($i < 5)
  13.    {
  14.         $retarray[$i]["id"] = $results[$i]["id"];
  15.         $retarray[$i]["title"] = $results[$i]["title"];
  16.         $retarray[$i]["introtext"] = $results[$i]["introtext"];
  17.         ++$i;
  18.     }
  19. }
  20. ?>
  21.  
  22. <html>
  23. <head>
  24. </head>
  25.  
  26. <body>
  27.     <b>Output: </b>
  28.  
  29.  
  30.     <table>
  31.     <?php 
  32.     $i = 0;
  33.     while ($i < count($retarray)){
  34.         print "<tr><td>".$retarray[$i]["id"]."</td></tr>";
  35.         print "<tr><td>".$retarray[$i]["title"]."</td></tr>";
  36.         print "<tr><td>".$retarray[$i]["introtext"]."</td></tr>";
  37.         ++$i;
  38.     }?>
  39.     </table>
  40.  
  41. </body>
  42. </html>
  43.  
  44.  

[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]
Jul 28 '07 #5
ilearneditonline
130 Expert 100+
Thanks, I've tried that but I'm still getting the same output, see bottom of post.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?
  3.  
  4.      $results=mysql_query("SELECT id, title, introtext FROM jos2_content WHERE `state` =1 AND `ordering` !=0 ORDER BY ordering ASC");
  5.  
  6.  
  7. if (count($results))
  8. {
  9.     $i = 0;
  10.     while ($i < count($results))
  11.     // or if you only wanted to return a total of 5 records
  12.     // while ($i < 5)
  13.    {
  14.         $retarray[$i]["id"] = $results[$i]["id"];
  15.         $retarray[$i]["title"] = $results[$i]["title"];
  16.         $retarray[$i]["introtext"] = $results[$i]["introtext"];
  17.         ++$i;
  18.     }
  19. }
  20. ?>
  21.  
  22. <html>
  23. <head>
  24. </head>
  25.  
  26. <body>
  27.     <b>Output: </b>
  28.  
  29.  
  30.     <table>
  31.     <?php 
  32.     $i = 0;
  33.     while ($i < count($retarray)){
  34.         print "<tr><td>".$retarray[$i]["id"]."</td></tr>";
  35.         print "<tr><td>".$retarray[$i]["title"]."</td></tr>";
  36.         print "<tr><td>".$retarray[$i]["introtext"]."</td></tr>";
  37.         ++$i;
  38.     }?>
  39.     </table>
  40.  
  41. </body>
  42. </html>
  43.  
  44.  

[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.
Expand|Select|Wrap|Line Numbers
  1. $i = 0;
  2. while $result = mysql_fetch_array($results)){
  3.     $retval[$i]["id"] = $result["id"];
  4.     $retval[$i]["title"] = $result["title"];
  5.     $retval[$i]["introtext"] = $result["introtext"];
  6.     ++$i;
  7. }
Jul 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
61
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...
2
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...
2
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...
3
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
117
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...
2
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...
1
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.