Jump to content

[SOLVED] Beginner help with displaying data


Xager

Recommended Posts

First: I don't know that much PHP so please excuse my ignorance.

 

I have been doing up a website which will, among other things, display news that I input into the MYSQL database. I have a working version of this where it just displays 5 posts per page in descending order (the latest at the top of the page) with the page numbering underneath them. Now what I wan't to do is just dispaly the latest 5 posts on the page but, I want to dispaly them so that I can put in a Javascript function to maxamize and minimize each post by clicking on the title.

 

So, I have, ID, Title, Newstype, Author, Post, Date. In my mind I see this as display each of these using the COUNT(*) thing and just subtracting 1, 2, 3, 4 to get the 5 posts I want. I just don't know how to do it.

 

I hope that made sense I almost don't understand it myself  ???

 

Thanks for any help

Regards,

Xager

Link to comment
Share on other sites

use the same query as pulling the data out when you display 5 per page but just use

 

 

select * from table

LIMIT 5

ORDER BY Date ASC

 

and there are your top 5 latest news posts, cod ewill look somthing like this

 

<?php
$query = mysql_query("select * from tablename LIMIT 5 ORDER BY Date ASC") or die('Error selecting top 5: '.mysql_error());
while ($result=mysql_fetch_array($query)) {
  //here is where you write your javascript dynamicly using $result['columnname'] to echo the row's data back
}
?>

 

 

Reghards

Liam

Link to comment
Share on other sites

thanks for the speedy responce, but...

 

I don't know enough PHP to really do this, which is why I am here, I am learning but not enough yet. So, this is what I have, it's basic at the moment but I am pretty sure its not going to do what I want.

<?php
include 'mysql_connect.php';
$sql = mysql_query("SELECT * FROM news_posts LIMIT 5 ORDER BY id DESC");
while($row = mysql_fetch_array($sql)){
echo "$row[title]</a> | $row[newstype]<br />$row[post]<br />$row[author] | $row[sd]<br /><hr width=100% /><br />";
}
?>

I probably should have explained this a bit more so:

 

What I want is to have the 5 posts, the top 2 will be "expanded" so that you can view all information contained, then the next 3 I want to be "minimized" so that all you see is the title with has a little + or - for coolness and the newstype. So, in that script that was mentioned in the above post it would display the code the same for all 5 instead of haveing seperate code for the "expanding" and "minimizing" sets.

 

Once again I hope this makes sense. I don't really know how to explain things much better.

 

Cheers,

Xager

Link to comment
Share on other sites

check out www.xager.net/V2/index.php which is what i want to do, excepts that instead of having them all minimized I want to have the top 2 expanded and then the next 3 minimized. Javascript does the maximize and minimize and PHP is pulling the information out of the database.

Link to comment
Share on other sites

could do with either looking at their code and ripping it out or searching google or posting on HTML/javascript forum as this isnt really a PHP issue, not saying noone will help you but generally people just support actual PHP say if you already have the code but somthing aint echoing back.

 

Just have a google for expand and collapse tables

 

 

Regards

Liam

Link to comment
Share on other sites

Thats not really what I meant. That website is mine so I already have the Javascript working, what I need is some PHP to display 5 posts. Not a script to dispaly 5 posts through LIMITs and ORDER, 5 posts so that I may change the way they look independantly. Sorry for my pretty crappy description.

Link to comment
Share on other sites

you will need to change your code a bit first off your body tag

 

<body onLoad="MM_preloadImages('images/wow-o.jpg','images/war-o.jpg','images/design-o.jpg','images/forum-o.jpg'); shoh(1); shoh(2);">

 

notice the extra shoh(1); and shoh(2);

 

but as we dont know what ID's your going to use u will have to have shoh(1); 2.. 3.. 4.. and 5 for each page instea dof the mysql ID (just guessing) so you will need $i++; or simular to create the assending shoh function and ID

 

If you have problems then post the exact code your using and i will modifie for you.

 

Regards

Liam

Link to comment
Share on other sites

Sort your query by Date, then use a for loop to grab the first five rows out of the query.

 

hes done than but needs the top 2 results expanded.

 

What i was just thinking as i was about to wash the pots (info you dont need tbh lol) was why not just not add the onclick function and div tags for the first 2 results therefore the first 2 will be expanded.

 

 

Liam

Link to comment
Share on other sites

I did get it working the the way I wanted it finally. The only this is there must be a better way to do it, because I don't know that much PHP I haven't got a clue how so I will post my code and hopefully get some ideas on how to clean it up and condense it down a bit.

 

<?php
include 'mysql_connect.php';
$from = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM news_posts"),0);
$idone = $from;
$idtwo = $from - 1;
$idthree = $from - 2;
$idfour = $from - 3;
$idfive = $from - 4;

$idonesql = mysql_query("SELECT id, title, newstype, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts WHERE id=$idone");
$commone = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM comments WHERE nid=$idone"),0);
while($row = mysql_fetch_array($idonesql)){
    echo "<img src=d.gif name=img$row[id] width=9 height=9 border=0>
		 <a href='javascript:voide(0)' onClick=shoh($row[id]);>$row[title]</a> | $row[newstype]<br /><div style=display:relative; id=$row[id]>$row[post]<br />$row[author] | $row[sd] | <a href=comment.php?id=$idone>Comment [$commone]</a></div><br /><hr width=100% /><br />";
}
$idtwosql = mysql_query("SELECT id, title, newstype, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts WHERE id=$idtwo");
$commtwo = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM comments WHERE nid=$idtwo"),0);
while($row = mysql_fetch_array($idtwosql)){
    echo "<img src=d.gif name=img$row[id] width=9 height=9 border=0>
		 <a href=javascript:voide(0) onClick=shoh($row[id]);>$row[title]</a> | $row[newstype]<br /><div style=display:relative; id=$row[id]>$row[post]<br />$row[author] | $row[sd] | <a href=comment.php?id=$idtwo>Comment [$commtwo]</a></div><br /><hr width=100% /><br />";
}
$idthreesql = mysql_query("SELECT id, title, newstype, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts WHERE id=$idthree");
$commthree = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM comments WHERE nid=$idthree"),0);
while($row = mysql_fetch_array($idthreesql)){
    echo "<img src=u.gif name=img$row[id] width=9 height=9 border=0>
		 <a href=javascript:voide(0) onClick=shoh($row[id]);>$row[title]</a> | $row[newstype]<br /><div style=display:none; id=$row[id]>$row[post]<br />$row[author] | $row[sd] | <a href=comment.php?id=$idthree>Comment [$commthree]</a></div><br /><hr width=100% /><br />";
}
$idfoursql = mysql_query("SELECT id, title, newstype, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts WHERE id=$idfour");
$commfour = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM comments WHERE nid=$idfour"),0);
while($row = mysql_fetch_array($idfoursql)){
    echo "<img src=u.gif name=img$row[id] width=9 height=9 border=0>
		 <a href=javascript:voide(0) onClick=shoh($row[id]);>$row[title]</a> | $row[newstype]<br /><div style=display:none; id=$row[id]>$row[post]<br />$row[author] | $row[sd] | <a href=comment.php?id=$idfour>Comment [$commfour]</a></div><br /><hr width=100% /><br />";
}
$idfivesql = mysql_query("SELECT id, title, newstype, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts WHERE id=$idfive");
$commfive = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM comments WHERE nid=$idfive"),0);
while($row = mysql_fetch_array($idfivesql)){
    echo "<img src=u.gif name=img$row[id] width=9 height=9 border=0>
		 <a href=javascript:voide(0) onClick=shoh($row[id]);>$row[title]</a> | $row[newstype]<br /><div style=display:none; id=$row[id]>$row[post]<br />$row[author] | $row[sd] | <a href=comment.php?id=$idfive>Comment [$commfive]</a></div><br /><hr width=100% /><br />";
}
?>

 

So, there it is, if anyone can let me know if there is an easier way to do it that would be great.

There is a working version of this on my website @ www.xager.net/V2/test.php

Cheers,

Xager

Link to comment
Share on other sites

Try this mate

 

<?php
include 'mysql_connect.php';



$sql = mysql_query("SELECT id, title, newstype, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd 
					 FROM news_posts
					 LIMIT 5
					 ORDER BY date DESC");
$i=0;
while($row = mysql_fetch_array($sql)){
  $comm = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM comments WHERE nid=".$row['id'].""),0);
  $i++;
  if ($i >= 2) {
    echo "<img src=d.gif name=img$row[id] width=9 height=9 border=0>
		 <a href='javascript:voide(0)' onClick=shoh($row[id]);>$row[title]</a> | $row[newstype]<br /><div style=display:relative; id=$row[id]>$row[post]<br />$row[author] | $row[sd] | <a href=comment.php?id=$idone>Comment [$comm]</a></div><br /><hr width=100% /><br />";
  } else {
    echo "<img src=u.gif name=img$row[id] width=9 height=9 border=0>
		 <a href=javascript:voide(0) onClick=shoh($row[id]);>$row[title]</a> | $row[newstype]<br /><div style=display:none; id=$row[id]>$row[post]<br />$row[author] | $row[sd] | <a href=comment.php?id=$idthree>Comment [$comm]</a></div><br /><hr width=100% /><br />";
  }
}
?>

 

Could need a litle tweek..

 

Regards

Liam

Link to comment
Share on other sites

awesome! I am pretty sure I understand that but I can't really figure out what the problem is, I get this:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xager/public_html/V2/test2.php on line 11

 

It's probably easy but I can't figure it out.

Link to comment
Share on other sites

change

 

$sql = mysql_query("SELECT id, title, newstype, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd 
					 FROM news_posts
					 LIMIT 5
					 ORDER BY date DESC");

 

to

 

$sql = mysql_query("SELECT id, title, newstype, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd 
					 FROM news_posts
					 LIMIT 5
					 ORDER BY date DESC") or die(mysql_error());

 

see what the error is

 

 

Liam

Link to comment
Share on other sites

ok I got this:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY date DESC' at line 1

 

thanks for your help mate really appreciate it.

Link to comment
Share on other sites

sorry i put limit 5 before order by.. needs to be other way

 

$sql = mysql_query("SELECT id, title, newstype, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd

FROM news_posts

ORDER BY date DESC

LIMIT 5") or die(mysql_error());

 

Sorry bout that, i always get that the wrong way..

 

Regards

Liam

Link to comment
Share on other sites

sorry another thing i did wrong in example

 

  if ($i >= 2) {

 

should be

 

  if ($i <= 2) {

 

 

still tho that would mean that the last 3 should have been expanded as it was the other way.. is the code i used in the if the right code to show expanded? i beleive it's just the display:relative; that makes it show?

 

Liam

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.