Jump to content

limit mysql results


daveh33

Recommended Posts

I have a script which has a mysql query which returns 20 results - I want to add some validation so if my variable ($var) is empty, it only allows the user to select the first 5 results

 

my code is: -

 

$sql = mysql_query("SELECT * FROM greetingcards_cards WHERE category='$cat' LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){   
        $image = $row['url'];
       $cardid = $row['cardid'];
        echo "<br><img src=\"$image\" /><br><a href=\"?function=sendmessage&cardid=$cardid\">Pick this card</a><br>";
}

 

I want it so that if the variable is empty, instead of displaying "Pick this card" it displays - "You must register to pick this card" and have a different hyperlink

 

Is this type of validation possible? if so how is it done?

Link to comment
https://forums.phpfreaks.com/topic/77452-limit-mysql-results/
Share on other sites

Something like this may work... please try

<?php $sql = mysql_query("SELECT * FROM greetingcards_cards WHERE category='$cat' LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){   
        $image = $row['url'];
       $cardid = $row['cardid'];
        if (!empty($var)) {
        echo "<br><img src=\"$image\" /><br><a href=\"?function=sendmessage&cardid=$cardid\">Pick this card</a><br>";
} else
{
  if ($max_result <= 5) {
   echo "<br><img src=\"$image\" /><br><a href=\"?function=sendmessage&cardid=$cardid\">Pick this card</a><br>";
} else {
echo "Your other links for records 6----20";
}
}
} ?>

Link to comment
https://forums.phpfreaks.com/topic/77452-limit-mysql-results/#findComment-392098
Share on other sites

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
if (!$cat) {
echo "<b>Pick your category below<br></b>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=A\">Animals</a><br>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=C\">Celebritys</a><br>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=F\">Funny</a><br>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=S\">Sexy Girls</a><br>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=B\">Sexy Men</a><br>";
echo "<br>";
}
$max_results = 5;
$from = (($page * $max_results) - $max_results); 
$sql = mysql_query("SELECT * FROM greetingcards_cards WHERE category='$cat' LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){   
        $image = $row['url'];
       $cardid = $row['cardid'];

        if (!empty($mobile)) {
        echo "<br><img src=\"$image\" /><br><a href=\"?function=sendmessage&cardid=$cardid\">Pick this card</a><br>";
} else
{
  if ($max_results <= 5) {
   echo "<br><img src=\"$image\" /><br><a href=\"?function=sendmessage&cardid=$cardid\">Pick this card</a><br>";
} else {
echo "< a href=\"handle.php?function=register\">Register now to send this gold card!</a>";
}
}
}
mysql_free_result($sql);
echo "<b><br><A href=\"?function=pickcard\">Pick a different category</a></b><br>";

$query = mysql_query("SELECT * from greetingcards_cards WHERE category='$cat'");
$total_results = mysql_num_rows($query);
$total_pages = ceil($total_results / $max_results);
echo "<br />";
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"sendmessage.php?function=pickcard&cat=$cat&page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo " $i ";
        } else {
            echo " <a href=\"sendmessage.php?function=pickcard&cat=$cat&page=$i\">$i</a> ";
    }
}
if($page < $total_pages){
    $next = ($page + 1);
    echo " <a href=\"sendmessage.php?function=pickcard&cat=$cat&page=$next\">Next>></a>";
}
echo "</center>";
}

Link to comment
https://forums.phpfreaks.com/topic/77452-limit-mysql-results/#findComment-392107
Share on other sites

Try this, hope it works..

 

<?php
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}
if (!$cat) {
echo "<b>Pick your category below<br></b>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=A\">Animals</a><br>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=C\">Celebritys</a><br>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=F\">Funny</a><br>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=S\">Sexy Girls</a><br>";
echo "<a href=\"sendmessage.php?function=pickcard&cat=B\">Sexy Men</a><br>";
echo "<br>";
}
$max_results = 5;
$from = (($page * $max_results) - $max_results); 
$sql = mysql_query("SELECT * FROM greetingcards_cards WHERE category='$cat' LIMIT $from, $max_results");

while($row = mysql_fetch_array($sql))
{   
        $image = $row['url'];
       $cardid = $row['cardid'];

        if (!empty($mobile)) 
	{
        echo "<br><img src=\"$image\" /><br><a href=\"?function=sendmessage&cardid=$cardid\">Pick this card</a><br>";
	} 
	else
	{
		  if ($max_results <= 5) 
		  {
		   echo "<br><img src=\"$image\" /><br><a href=\"?function=sendmessage&cardid=$cardid\">Pick this card</a><br>";
		  } 
		  else 
		  {
			echo "< a href=\"handle.php?function=register\">Register now to send this gold card!</a>";
		  }
	}
}

mysql_free_result($sql);
echo "<b><br><A href=\"?function=pickcard\">Pick a different category</a></b><br>";

$query = mysql_query("SELECT * from greetingcards_cards WHERE category='$cat'");
$total_results = mysql_num_rows($query);
$total_pages = ceil($total_results / $max_results);
echo "<br />";
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"sendmessage.php?function=pickcard&cat=$cat&page=$prev\"><<Previous</a> ";
}
for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo " $i ";
        } else {
            echo " <a href=\"sendmessage.php?function=pickcard&cat=$cat&page=$i\">$i</a> ";
    }
}
if($page < $total_pages){
    $next = ($page + 1);
    echo " <a href=\"sendmessage.php?function=pickcard&cat=$cat&page=$next\">Next>></a>";
}
echo "</center>";

?>

Link to comment
https://forums.phpfreaks.com/topic/77452-limit-mysql-results/#findComment-392111
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.