need urgent help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phppro62
Forum Newbie
Posts: 16
Joined: Wed Oct 27, 2010 2:45 am

need urgent help

Post by phppro62 »

please I need someone to help
I need a code for sorting categories in the index page
i retrive data from table
$query1 = "SELECT * FROM users WHERE category='Arts' ;
Now I have many links
<li><a href="#">News</a></li>
<li><a href="#">Sports</a></li>
<li><a href="#">Jokes</a></li>
I want to click on the link news to sort the table to News
and so on
this this could be done?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: need urgent help

Post by social_experiment »

You need to pass the value according to which you want to sort along in a query string.

Code: Select all

<li><a href="page.php?value=News">News</a></li>
Then you need to retrieve the value

Code: Select all

<?php $value = $_GET['value']; ?>
Make sure it's sanitize (cleaned-up) before using it inside your query

Code: Select all

<?php
 $value = $_GET['value'];
 
 $query = mysql_query("SELECT fields FROM table ORDER BY ". $value ." ");
 
 // display the results
 while ($array = mysql_fetch_array($query)) {
 // show results
 }
?>
Use this as an idea and create your own script and paste it if you need more help. Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: need urgent help

Post by Neilos »

I'm not sure I understand your problem fully, but I'll give it a go.

I would do something like this;

Code: Select all

<style type="text/css">
a.news {}
a.sports {display: none;}
a.jokes {display: none;}
#news {position:absolute; top:50px; left:0px;}
#sports {position:absolute; top:50px; left:0px;}
#jokes {position:absolute; top:50px; left:0px;}
</style>

<script type="text/javascript" src="jquery_1_4_4.js"></script>
<script type="text/javascript">
$(document).ready(function() {

	//Click event on news
	$("#nav_bar a .news").click(function() { //Click on news and...
		$('#news').show(); // show news panel
		$("#sports").hide(); //hide sports panel
		$("#jokes").hide(); //hide jokes panel
	});

	//Click event on sports
	$("#nav_bar a .sports").click(function() { //Click on news and...
		$('#news').hide(); // hide news panel
		$("#sports").show(); //hide sports panel
		$("#jokes").hide(); //hide jokes panel
	});

	//Click event on jokes
	$("#nav_bar a .jokes").click(function() { //Click on news and...
		$('#news').hide(); // hide news panel
		$("#sports").hide(); //hide sports panel
		$("#jokes").show(); //show jokes panel
	});

});
</script>

<?php
include ('connecttodb.php');

$query1 = "SELECT * FROM news"; // I am assuming that this will select all the news stories
$query2 = "SELECT * FROM sports"; // I am assuming that this will select all the sports stories
$query3 = "SELECT * FROM jokes"; // I am assuming that this will select all the jokes

$result1 = mysql_query($query1);
$result2 = mysql_query($query2);
$result3 = mysql_query($query3);

$num1=mysql_num_rows($result1);
$num2=mysql_num_rows($result2);
$num3=mysql_num_rows($result3);

echo '<div id="nav_bar">';
echo '<a href="#" class="news">News</a>';
echo '<a href="#" class="sports">Sports</a>';
echo '<a href="#" class="jokes">Jokes</a>';
echo '</div>';

echo '<div id="news">';
$i=0;
while ($i < $num1) {
	$row1 = mysql_result($result1);
	$row1assoc = mysql_fetch_array($row1, MYSQL_ASSOC);
	echo '<li class="title">' . $row1assoc['title'] . '</li><br />';
	echo '<li class="content">' . $row1assoc['content'] . '</li><br />';
	$i++;
}
echo '</div>';

echo '<div id="sports">';
$j=0;
while ($j < $num2) {
	$row2 = mysql_result($result2);
	$row2assoc = mysql_fetch_array($row2, MYSQL_ASSOC);
	echo '<li class="title">' . $row2assoc['title'] . '</li><br />';
	echo '<li class="content">' . $row2assoc['content'] . '</li><br />';
	$j++;
}
echo '</div>';

echo '<div id="jokes">';
$k=0;
while ($k < $num3) {
	$row3 = mysql_result($result3);
	$row3assoc = mysql_fetch_array($row3, MYSQL_ASSOC);
	echo '<li class="title">' . $row3assoc['title'] . '</li><br />';
	echo '<li class="content">' . $row3assoc['content'] . '</li><br />';
	$k++;
}
echo '</div>';

?>
lol you're lucky, I got in to it and decided to write a full example. (untested)
Last edited by Neilos on Tue Jan 04, 2011 5:21 pm, edited 3 times in total.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: need urgent help

Post by Neilos »

damn my code is good looking, and all in 30 mins!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: need urgent help

Post by social_experiment »

phppro62 wrote:I want to click on the link news to sort the table to News
It looks like he wants to order his results ;) .mysql_num_rows() instead of mysql_numrows() ;)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: need urgent help

Post by Neilos »

lol it's all good practice. It's a little confusing what he wants from his question.
phppro62 wrote: I want to click on the link news to sort the table to News
Now I would say that means, he gets lots of news stories and he wants to display a list of news stories instead of jokes or sports stories.
phppro62 wrote: $query1 = "SELECT * FROM users WHERE category='Arts' ;
I have no idea what this means, apart from the sql command itself, what it actually pulls is a mystery 8O lol
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: need urgent help

Post by social_experiment »

Neilos wrote:It's a little confusing what he wants from his question.
True, if you look at the verb sort it seems that he is implying to order somehow and from the SELECT query it looks like he wants everything displayed.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: need urgent help

Post by Neilos »

social_experiment wrote:mysql_num_rows() instead of mysql_numrows() ;)
lol fixed
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: need urgent help

Post by Neilos »

social_experiment wrote:
Neilos wrote:It's a little confusing what he wants from his question.
True, if you look at the verb sort it seems that he is implying to order somehow and from the SELECT query it looks like he wants everything displayed.
Maybe, but until we get some clarification on what he is doing and/or wants we'll not know. It's like fishing, now is the waiting game! :lol: I wonder how urgent urgent is.
phppro62
Forum Newbie
Posts: 16
Joined: Wed Oct 27, 2010 2:45 am

Re: need urgent help

Post by phppro62 »

hi mates
thanks all for your time and efforts
I'll try to clarify what I need .

i have one page called index.php
i make this page dynamic using php and mysql
the page now is displaying data from the table users orderded by timesofsubmit, Des
there are submission for many categories such as News Arts Music and so on

I have nav bar that has text link <a href="#">News</a>
I want to make this link change the displaying data with (WHERE category='News')
I can do it for the loading of the page but don't know how to change it dynamically when there is a click on the link "News" to change from another category to what i want.
I hope that i clarify what i need

I can make it by pages every link directed to another page and display like news.php
but this is not a pratical way and takes time
phppro62
Forum Newbie
Posts: 16
Joined: Wed Oct 27, 2010 2:45 am

Re: need urgent help

Post by phppro62 »

Hi mates again
II tried the way that social_experiment gave me .
Yes it's working
Thsank you very much
Post Reply