Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieving variables and using them throughout output 1

Status
Not open for further replies.

theredqueen

Programmer
Oct 26, 2001
40
GB
Hey there, i'm trying to program a news script to retrieve the news items from a database, so i don't have to code the html for each one. I've done stuff in php before, but not with my database. Truth be told, I'm not entirely competent in it.

So anyway, i'd like to display the last 5 entries to the news table. I have 3 variables in the table, id, title and message.

so, this is all ok, but i think it's not what i need...
<?php
/* Connecting, selecting database */
$link = mysql_connect(&quot;localhost&quot;, &quot;***mydb&quot;, &quot;***mypass&quot;)
or die(&quot;Could not connect&quot;);
mysql_select_db(&quot;***mydb&quot;) or die(&quot;Could not select database&quot;);

/* Performing SQL query */
$query = &quot;SELECT * FROM news&quot;;
$result = mysql_query($query) or die(&quot;Query failed&quot;);


then i wanted to print the variables as is into my html...

print &quot; <table width=\&quot;718\&quot; border=\&quot;0\&quot; cellpadding=\&quot;0\&quot; cellspacing=\&quot;0\&quot;>\n&quot;;
print &quot; <tr> \n&quot;;
print &quot; <td width=\&quot;176\&quot; height=\&quot;15\&quot;></td>\n&quot;;
print &quot; <td width=\&quot;372\&quot; valign=\&quot;top\&quot;><font size=\&quot;2\&quot;>$title</font></td>\n&quot;;
print &quot; <td width=\&quot;170\&quot;></td>\n&quot;;
print &quot; </tr>\n&quot;;

i know this won't work as is, and i think i need to use

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

but to be honest, it confuses me. is there no way i can retrieve the variables then just refer to them in my html as above?
i'm also still a bit confused about how i'm going to get just the last 5, i was guessing something with some thing like the last id -5 or soemthing, then i'd have to link at the bottom to a page with -5 to -10.

i appreciate this is a number of fairly lame questions, but would also appreciate any help! i am not friends with php =D
 
To get just the 5 last news entries only, you could change your SQL query to the following:
if (!isset($_GET['start']))
$_GET['start'] = 0;
$result = mysql_query(&quot;SELECT id, title, message FROM news ORDER BY id DESC LIMIT &quot; . ½_GET['start'] . &quot;, 5&quot;);
Then to get variables as $id, $title and $message, you could change your while loop to:
while (list($id, $title, $message) = mysql_fetch_row($result))
{
// Print HTML here
}
Then to link to the next page, just do a link like
$nextstart = $_GET['start'] + 5;
echo &quot;<a href=\&quot;&quot; . $_SERVER['PHP_SELF'] . &quot;?start=$nextstart\&quot;>Next page</a>&quot;;
I hope this gets you in the right direction. //Daniel
 
Oops, a typo in my previous post. Replace
$result = mysql_query(&quot;SELECT id, title, message FROM news ORDER BY id DESC LIMIT &quot; . ½_GET['start'] . &quot;, 5&quot;);
with
$result = mysql_query(&quot;SELECT id, title, message FROM news ORDER BY id DESC LIMIT &quot; . $_GET['start'] . &quot;, 5&quot;); //Daniel
 
wow that works fabulously!
you are an absolute star...speaking of which, have one for each post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top