Jump to content


Photo

PHP/SQL Display Help


  • Please log in to reply
14 replies to this topic

#1 teki

teki
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 05 May 2006 - 10:23 PM

Hey guys

Recently i have been coding a php code which auto submits to a database.

Can anyone supply me with a code of

Showing the lastest article with a read more option

Showing the latest ten with read more

Showing all with read more

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 06 May 2006 - 08:42 AM

Asuming each record has a unique auto_incrementing id column, then

for all articles (latest first)
$sql = "SELECT id, title FROM articles ORDER BY id DESC";

for latest x articles to be displayed

$x = 10;
$sql = "SELECT id, title FROM articles ORDER BY id DESC LIMIT $x";


For the read more bit, output a link containing the id of the record in the querystring

echo "a href='showmore.php?id=$id>Read more...</a>";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 teki

teki
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 06 May 2006 - 09:55 AM

[!--quoteo(post=371789:date=May 6 2006, 06:42 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 6 2006, 06:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Asuming each record has a unique auto_incrementing id column, then

for all articles (latest first)
$sql = "SELECT id, title FROM articles ORDER BY id DESC";

for latest x articles to be displayed

$x = 10;
$sql = "SELECT id, title FROM articles ORDER BY id DESC LIMIT $x";
For the read more bit, output a link containing the id of the record in the querystring

echo "a href='showmore.php?id=$id>Read more...</a>";
[/quote]
Thanks and is that code auto going to connect to the right database?

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 06 May 2006 - 11:16 AM

You still need to connect to your server and select the database, but as you are already writing to it I assume you know how to do that.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 teki

teki
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 06 May 2006 - 10:17 PM

[!--quoteo(post=371802:date=May 6 2006, 09:16 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 6 2006, 09:16 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You still need to connect to your server and select the database, but as you are already writing to it I assume you know how to do that.
[/quote]
Ok Cheers mate

echo "a href='showmore.php?id=$id>Read more...</a>";

With taht code u gave me does there need to be more information/ coding in a file named showmore.php

#6 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 07 May 2006 - 12:13 AM

YOU NEED A PAGE NAMED SHOWMORE.PHP..THAT WHERE IS ASSUMING YOU GOT THE REST OF THE ARTICLE..

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 May 2006 - 09:27 AM

[!--quoteo(post=371897:date=May 6 2006, 11:17 PM:name=teki)--][div class=\'quotetop\']QUOTE(teki @ May 6 2006, 11:17 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Ok Cheers mate

echo "a href='showmore.php?id=$id>Read more...</a>";

With taht code u gave me does there need to be more information/ coding in a file named showmore.php
[/quote]

When the user clicks the "Read more..." link the page "showmore.php" (or whatever you want to call it) will be loaded and the id of the article is passed to it in the querystring. That page then has to query the database table, using the id value, to display the article contents in full.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 teki

teki
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 07 May 2006 - 11:20 AM

[!--quoteo(post=371970:date=May 7 2006, 07:27 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 7 2006, 07:27 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
When the user clicks the "Read more..." link the page "showmore.php" (or whatever you want to call it) will be loaded and the id of the article is passed to it in the querystring. That page then has to query the database table, using the id value, to display the article contents in full.
[/quote]
ok thanks and one last question

With showing the differnt vairables on the pageh which shows the full article do you need to put lots of echos?

#9 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 May 2006 - 12:45 PM

One or many - depends on your coding style, for example

You can

<?php
echo "<H1>$title<H1>
    <H3>Author : $author</H3>
    <p>$content</p>
    <p>Date : $article_date</p>";
?>    

or

<H1><?php echo $title ?><H1>
<H3>Author : <?php echo $author ?></H3>
<p><?php echo $content ?></p>
<p>Date : <?php echo $article_date ?></p>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#10 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 07 May 2006 - 02:00 PM

understanding this concept is what really opened php up for me.

on the show more page you have to run another query based on the id passed in the url
showmore.php?id=15


$id = $_GET['id']; //get the id from the querystring
$query = "SELECT * FROM table WHERE id = ". $id ."";

that query will return article number 15

#11 teki

teki
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 08 May 2006 - 02:29 AM

[!--quoteo(post=371991:date=May 7 2006, 10:45 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 7 2006, 10:45 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
One or many - depends on your coding style, for example

You can

<?php
echo "<H1>$title<H1>
    <H3>Author : $author</H3>
    <p>$content</p>
    <p>Date : $article_date</p>";
?>    

or

<H1><?php echo $title ?><H1>
<H3>Author : <?php echo $author ?></H3>
<p><?php echo $content ?></p>
<p>Date : <?php echo $article_date ?></p>
[/quote]

Ok thanks and to put them on different parts of a page u could put em in tables right?

[!--quoteo(post=371999:date=May 8 2006, 12:00 AM:name=emehrkay)--][div class=\'quotetop\']QUOTE(emehrkay @ May 8 2006, 12:00 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
understanding this concept is what really opened php up for me.

on the show more page you have to run another query based on the id passed in the url
showmore.php?id=15
$id = $_GET['id']; //get the id from the querystring
$query = "SELECT * FROM table WHERE id = ". $id ."";

that query will return article number 15
[/quote]
So would i mix both of your codes together?

And would i need to put a connecting to database on this page?

#12 teki

teki
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 May 2006 - 05:45 AM

Anyone know?

#13 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 09 May 2006 - 06:56 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]And would i need to put a connecting to database on this page?[/quote]

Yes, you need to connect on each page that accesses the db
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#14 teki

teki
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 May 2006 - 08:38 AM

[!--quoteo(post=372521:date=May 9 2006, 04:56 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ May 9 2006, 04:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Yes, you need to connect on each page that accesses the db
[/quote]
also barand do i need to use the code emehrkay gave me? The one that goes
$id = $_GET['id']; //get the id from the querystring
$query = "SELECT * FROM table WHERE id = ". $id ."";

Also would the code for showing latest article be
$x = 1;
$sql = "SELECT id, title FROM articles ORDER BY id DESC LIMIT $x";
and if you had five variables in the article and you only wanted to show 2 of them with that latest one article code what would you add to that code?

#15 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 09 May 2006 - 08:23 PM

You'll need code like emehrkay posted for getting that specific article from the "read more..." link.

In the SELECT clause try to avoid "SELECT * " as this will get all the columns and just specify the ones you actually need. The less data you pull from the database the quicker the query. Listing the actual columns also better documents what the query is doing if you come back to it later or if someone else needs to change something.

A series of articles by Kevin Yank got me started with PHP/MySQL - worth a read.

[a href=\"http://www.sitepoint.com/article/publishing-mysql-data-web\" target=\"_blank\"]http://www.sitepoint.com/article/publishing-mysql-data-web[/a]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users