Jump to content


Photo

Database Query Display


  • Please log in to reply
25 replies to this topic

#1 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 23 June 2006 - 02:59 PM

Hi Guys,

I was able to post the content of my database, however I would like to put a maximum number of display, beause I want to put at my left bar of my website, I want to display only the 5 recent articles, then put more articles on the bottom of the left side bar. When I click more article it should show all articles.

Also, How can I make link to the title so it go to the full article? :) eg. because I only want to display:

date:
title

date:
title

date:
title

when I click the title it will go to the full artile which is
date:
title
article:

Here's my script.
[a href=\"http://www.leeph.net/test/view_news.txt\" target=\"_blank\"]http://www.leeph.net/test/view_news.txt[/a]


Hope you guys could help me out :)

Thanks.

#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 23 June 2006 - 03:09 PM

Change:
$query = 'SELECT * FROM news ORDER BY date DESC';
To:
$query = 'SELECT * FROM news ORDER BY date DESC LIMIT 5';


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 June 2006 - 03:27 PM

To do what you want to do place the following bit of code:
// check whether the 'more' link has not been clicked.
// if it hasn't we'll only show the five most recent news articles
if(!isset($_GET['more']) || $_GET['more'] == 0)
{
    // this tells MySQL to select only 5 results
    $query .= " LIMIT 5";
}
after this line:
$query = 'SELECT * FROM news ORDER BY date DESC';

Now to add the link you'll need this code:
<a href="?more=1">View all</a>
This will need to be placed after you news article is displayed, proabably after the while loop if I'm correct.

So now what will happen is PHP will check 'more' has been set in the url and that it is set to '1'. If more is not set in the url it'll append 'LIMIT 5' to end of the query. This will then only show 5 news articles. But if more is set in the url and is set 1 it'll show all the articles.

Now to linke the title you'll need to do this:
<h3><a href=\"news.php?id={$row['vc_title']}\">{$row['vc_title']}</a></h3>
Change news.php to the correct file you want the user to go to

#4 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 June 2006 - 12:32 AM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Now to add the link you'll need this code:
<a href="?more=1">View all</a>
This will need to be placed after you news article is displayed, proabably after the while loop if I'm correct.
[/quote]

I tried to add it, but when I point the link, its pointed to [a href=\"http://localhost/php/tnr/index.php?more=1\" target=\"_blank\"]http://localhost/php/tnr/index.php?more=1[/a] is that correct? when I click it, nothing happens.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Now to linke the title you'll need to do this:
<h3><a href=\"news.php?id={$row['vc_title']}\">{$row['vc_title']}</a></h3>
Change news.php to the correct file you want the user to go to
[/quote]

I changed the id={$row['vc_title']}\">{$row['vc_title']} to id={$row['int_id']}\">{$row['vc_title']}
so when I point the link it points to [a href=\"http://localhost/php/tnr/news/news.php?id=37\" target=\"_blank\"]http://localhost/php/tnr/news/news.php?id=37[/a] however, I dont know what syntax to put in news.php, can you guys instruct me? based on [a href=\"http://www.leeph.net/test/view_news.txt\" target=\"_blank\"]http://www.leeph.net/test/view_news.txt[/a]


Thanks and hope you guys could teach me, sorry im newbie to php.

#5 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 24 June 2006 - 01:34 AM

Sorry, yesterday I didn't get to know what you trying to say, maybe I'm kinda tired..

From the index page where you show your news,
point yur title url to $row['int_id'].

Below is the title on your index page:
<a href=\"news.php?id={$row['int_id']}\">$row['vc_title']</a>


Create a news.php, and put this code in.
<?php
if (!isset($_GET['news'] || $_GET['news'] == 0)) {
    echo "You didn't select any article.";
    } else {
        $_GET['news'] = $news;
        $query = "SELECT * FROM news WHERE int_id ='$news'";
        $r = mysql_query($query) or die("Query failed: " . mysql_error());
        $row = mysql_fetch_assoc($r);
        echo $row['date']; // Remember to change date, i'm not sure of your table structure..
        echo "<p><h3>".$row['vc_title']."</h3>";
        echo "".$row['vc_article']."<br /></p>";
      }
?>

Tell me if there's any more problem, ps.. I'm still in a sleepy mode.. lolz. :s

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#6 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 June 2006 - 11:41 AM

[!--quoteo(post=387340:date=Jun 23 2006, 08:34 PM:name=hackerkts)--][div class=\'quotetop\']QUOTE(hackerkts @ Jun 23 2006, 08:34 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Sorry, yesterday I didn't get to know what you trying to say, maybe I'm kinda tired..

From the index page where you show your news,
point yur title url to $row['int_id'].

Below is the title on your index page:
<a href=\"news.php?id={$row['int_id']}\">$row['vc_title']</a>
Create a news.php, and put this code in.
<?php
if (!isset($_GET['news'] || $_GET['news'] == 0)) {
    echo "You didn't select any article.";
    } else {
        $_GET['news'] = $news;
        $query = "SELECT * FROM news WHERE int_id ='$news'";
        $r = mysql_query($query) or die("Query failed: " . mysql_error());
        $row = mysql_fetch_assoc($r);
        echo $row['date']; // Remember to change date, i'm not sure of your table structure..
        echo "<p><h3>".$row['vc_title']."</h3>";
        echo "".$row['vc_article']."<br /></p>";
      }
?>

Tell me if there's any more problem, ps.. I'm still in a sleepy mode.. lolz. :s
[/quote]

Hi hackerkts,

Thanks, I will try this :)

Regards,
Lee


#7 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 June 2006 - 01:48 PM

Hi! I tried to put this code to /news/news.php

<?php
require_once(mysql_connect.php);

ini_set ('display_errors', 1);

error_reporting (E_ALL & ~E_NOTICE);
    
if (!isset($_GET['news']) || $_GET['news'] == 0) {
    echo "You didn't select any article.";
    } else {
        $_GET['news'] = $news;
        $query = "SELECT * FROM news WHERE int_id ='$news'";
        $r = mysql_query($query) or die("Query failed: " . mysql_error());
        $row = mysql_fetch_array($r);
        echo $row['date']; 
        echo "<p><h3>".$row['vc_title']."</h3>";
        echo "".$row['vc_article']."<br /></p>";
      }
?>

but I its just showed my "You didnt select any article"

I used this code from index.php
<a href=\"news/news.php?id={$row['int_id']}\">{$row['vc_title']}</a>

Any comments? :)

thanks and really appreciated your help!

#8 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 24 June 2006 - 02:06 PM

When you clicked on the title, what link it show ?

Edit:
Ok I know why, it's my mistake. Morning just woke up not long and write out that script.

Now change:
if (!isset($_GET['news']) || $_GET['news'] == 0) {
To:
if (!isset($_GET['id']) || $_GET['id'] == 0) {

Edit2:
If you still having problem with it, try checking the scripts below.

Database:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--]CREATE TABLE `news` (
`id` tinyint(10) NOT NULL auto_increment,
`date` text NOT NULL,
`title` text NOT NULL,
`article` text NOT NULL,
PRIMARY KEY (`id`)
) [!--sql2--][/div][!--sql3--]

config.php (remember to change the config, I just setup it using my local computer)
<?php
$host = "localhost";
$username = "root";
$password = "";
$db = "help";

mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_close;
?>

index.php
<?php

require_once('config.php');
$query = "SELECT * FROM `news` ORDER BY id DESC LIMIT 5";
$result = mysql_query($query) or die("Query failed: " . mysql_error());
while($row = mysql_fetch_assoc($result)) {
    echo "".$row['date']."<br>";
    echo "<a href='news.php?id=".$row['id']."'>".$row['title']."</a><br><br>";
}

?>

news.php
<?php

require_once('config.php');
$news = $_GET['id'];
$query = "SELECT * FROM `news` WHERE id ='$news'";
$result = mysql_query($query) or die("Query failed: " . mysql_error());
$row = mysql_fetch_assoc($result);
if (!isset($news) || $news == 0) {
    echo "You didn't select any news.";
    } else {
        echo "".$row['date']."<br>";
        echo "".$row['title']."<br>";
        echo $row['article'];
        }

?>


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#9 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 June 2006 - 02:47 PM

Hi hackerkts,

Thank you so much for your help! :) I will try this out. hope all get well. :)

Really appreacited it a lot :)

Regards,
Lee

#10 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 24 June 2006 - 02:50 PM

No problem, I'm always glad to help out.
If you had any problem with it, feel free to reply or drop me a msg in msn.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#11 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 June 2006 - 02:57 PM

[!--quoteo(post=387452:date=Jun 24 2006, 09:50 AM:name=hackerkts)--][div class=\'quotetop\']QUOTE(hackerkts @ Jun 24 2006, 09:50 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
No problem, I'm always glad to help out.
If you had any problem with it, feel free to reply or drop me a msg in msn.
[/quote]

Okay,, thanks again :)

#12 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 June 2006 - 03:48 PM

Still not working for on me :(

The link on index.php is [a href=\"http://localhost/php/tnr/news/news.php?id=12\" target=\"_blank\"]http://localhost/php/tnr/news/news.php?id=12[/a]


Index.php (works)
<?php 

ini_set ('display_errors', 1);

error_reporting (E_ALL & ~E_NOTICE);



if(!isset($_GET['more']) || $_GET['more'] == 0)    
$query = 'SELECT * FROM `news` ORDER BY date DESC LIMIT 5';



if ($r = mysql_query ($query)) { 
    

    while($row = mysql_fetch_assoc($r)) {

     echo "".$row['date']."<br>";
    echo "<a href='news/news.php?id=".$row['int_id']."'>".$row['vc_title']."</a><br><br>";

    }

    

    } else { 

    die ('<p>Could create the table because: <b>' . mysql_error() . "</b>. The query was $query.</p>");

} 


mysql_close(); // Close the database connection.



?>

Here's my news/news.php. I encounter error and it doesnt display the query.
<?php

$news = $_GET['int_id'];
$query = "SELECT * FROM `news` WHERE int_id ='$news'";
$result = mysql_query($query) or die("Query failed: " . mysql_error());
$row = mysql_fetch_assoc($r);
if (!isset($_GET['int_id']) || $_GET['int_id'] == 0) {
    echo "You didn't select any news.";
    } else {
        echo "".$row['date']."<br>";
        echo "".$row['vc_title']."<br>";
        echo $row['vc_article'];
        }
?>
error i encounter is:
an error occurred in script /media/data/projects/WebApps/php/tnr/include/config.inc on line 14: Undefined index: int_idAn error occurred in script /media/data/projects/WebApps/php/tnr/include/config.inc on line 14: Undefined variable: rAn error occurred in script /media/data/projects/WebApps/php/tnr/include/config.inc on line 14: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resourceYou didn't select any news.

Here's my config.inc
<?php 

error_reporting (0); // Production level
error_reporting (E_ALL); // Development level


function my_error_handler ($e_number, $e_message) {

    $message = 'An error occurred in script ' . __FILE__ . ' on line ' . __LINE__ . ": $e_message";
    echo '<font color="red" size="+1">', $message, '</font>'; // Development (print the error in red)
}
set_error_handler('my_error_handler');
?>

My Database is
int_id = tinyint(10)
vc_title = text
vc_article = text
date = date

Thanks again and again for the help! :)

#13 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 24 June 2006 - 04:18 PM

Your news.php, you need to make a connection to MySQL.
It would be better if you put the setting in config.php, so next time you wanna connect to MySQL you just require/include that file.

As for the error, I'm not very sure about it.
Try to set a connect in news.php and see if the error still there.

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#14 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 June 2006 - 05:18 PM

[!--quoteo(post=387490:date=Jun 24 2006, 11:18 AM:name=hackerkts)--][div class=\'quotetop\']QUOTE(hackerkts @ Jun 24 2006, 11:18 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Your news.php, you need to make a connection to MySQL.
It would be better if you put the setting in config.php, so next time you wanna connect to MySQL you just require/include that file.

As for the error, I'm not very sure about it.
Try to set a connect in news.php and see if the error still there.
[/quote]

Yes, I have included require_once('mysql_connect.php'); i just didnt include it on my post, sorry.

on news.php i have:
require_once('include/mysql_connect.php');
require_once('include/getnews.php')

where getnews.php contains
<?php

$news = $_GET['int_id'];
$query = "SELECT * FROM `news` WHERE int_id ='$news'";
$result = mysql_query($query) or die("Query failed: " . mysql_error());
$row = mysql_fetch_assoc($r);
if (!isset($_GET['int_id']) || $_GET['int_id'] == 0) {
    echo "You didn't select any news.";
    } else {
        echo "".$row['date']."<br>";
        echo "".$row['vc_title']."<br>";
        echo $row['vc_article'];
        }
?>


any other suggestion? :)



#15 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 24 June 2006 - 05:43 PM

try know
<?php

$news = $_GET['int_id'];
$query = "SELECT * FROM `news` WHERE int_id ='$news'";
$r= mysql_query($query) or die("Query failed: " . mysql_error());
$row = mysql_fetch_assoc($r);
if (!isset($_GET['int_id']) || $_GET['int_id'] == 0) {
    echo "You didn't select any news.";
    } else {
        echo "".$row['date']."<br>";
        echo "".$row['vc_title']."<br>";
        echo $row['vc_article'];
        }
?>


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#16 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 June 2006 - 07:28 PM

Hi I just fixed the script and it worked..

Here's the working code:
<?php
$news = $_GET['id'];
$query = "SELECT * FROM news WHERE int_id = '$news'";
$r = mysql_query($query) or die("Query failed: " . mysql_error());
$row = mysql_fetch_assoc($r);
if (!isset($_GET['id']) || $_GET['id'] == 0) {
    echo "You didn't select any news.";
    } else {
        echo "".$row['date']."<br>";
        echo "".($row['vc_title'])."<br>"; 
        echo nl2br($row['vc_article']);
    }
?>


Thanks a lot for the hellp guys :)

#17 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 24 June 2006 - 08:55 PM

Now, I am trying to echo something like. "sorry no data has found on the database" of the database is empty. I want to put it on this code

<?php 
ini_set ('display_errors', 1);

error_reporting (E_ALL & ~E_NOTICE);


$query = 'SELECT * FROM news ORDER BY date DESC';

if ($r = mysql_query ($query)) { // Run the query.


while ($row = mysql_fetch_array ($r)) { 
    echo "<b>Date:</b> ".$row['date']."<br>";
    echo "<a href='news.php?id=".$row['int_id']."'><font size = 3>".$row['vc_title']."</font></a><br><br>";
    echo "<font size = 2>Reference Number: " .$row['int_id']."</font><br><hr>";


    }



} else { // Query didn't run.

    die ('<p>Could create the table because: <b>' . mysql_error() . "</b>. The query was $query.</p>");

} // End of query IF.



mysql_close(); // Close the database connection.



?>

can somebody tell me how? :)

thanks

#18 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 25 June 2006 - 03:43 AM

Do you mean when your news is totally empty then it display the words ?

Try this, and I have modify abit of your script.
<?php
ini_set ('display_errors', 1);

error_reporting (E_ALL & ~E_NOTICE);


$query = 'SELECT * FROM news ORDER BY date DESC';

$r = mysql_query ($query) or die('<p>Could create the table because: <b>' . mysql_error() . "</b>. The query was $query.</p>");

$num_rows = mysql_num_rows($r);
if (!$num_rows || $num_rows == 0) {
    echo "sorry no data has found on the database";
    } else {


while ($row = mysql_fetch_array ($r)) {
    echo "<b>Date:</b> ".$row['date']."<br>";
    echo "<a href='news.php?id=".$row['int_id']."'><font size = 3>".$row['vc_title']."</font></a><br><br>";
    echo "<font size = 2>Reference Number: " .$row['int_id']."</font><br><hr>";
    }
} // End if statements

mysql_close(); // Close the database connection.



?>


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#19 wizzkid

wizzkid
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 25 June 2006 - 11:33 AM

[!--quoteo(post=387650:date=Jun 24 2006, 10:43 PM:name=hackerkts)--][div class=\'quotetop\']QUOTE(hackerkts @ Jun 24 2006, 10:43 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Do you mean when your news is totally empty then it display the words ?

Try this, and I have modify abit of your script.
[/quote]

Yes, Thanks again for the help! :)

how about for this code: (with totally empty as well)
<?php
$news = $_GET['id'];
$query = "SELECT * FROM news WHERE int_id = '$news'";
$r = mysql_query($query) or die("Query failed: " . mysql_error());
$row = mysql_fetch_assoc($r);
if (!isset($_GET['id']) || $_GET['id'] == 0) {
    echo "You didn't select any news.";
    } else {
        echo "".$row['date']."<br>";
        echo "".($row['vc_title'])."<br>";
        echo nl2br($row['vc_article']);
    }
?>

Thanks a million! :)

#20 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 25 June 2006 - 11:49 AM

>.<! It couldn't be totally empty, is that the full script in your news.php ?

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users