daveh33 Posted November 15, 2007 Share Posted November 15, 2007 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? Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 15, 2007 Share Posted November 15, 2007 Yes, you need to keep it inside if...else like <?php if (empty($var)) { echo "<br><img src=\"$image\" /><br><a href=\"?function=sendmessage&cardid=$cardid\">Pick this card</a><br>"; } else { echo "something else"; } ?> Quote Link to comment Share on other sites More sharing options...
daveh33 Posted November 15, 2007 Author Share Posted November 15, 2007 yes but that will validate the entire set of results - I want everyone to have access to the first 5 - then display something else after those 5 results Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 15, 2007 Share Posted November 15, 2007 Then you have to check $var before the query, if (empty($var)) { $max_results = 5; } else { $max_results = 20; // if max_result is your limit } Quote Link to comment Share on other sites More sharing options...
daveh33 Posted November 15, 2007 Author Share Posted November 15, 2007 If $var is empty - I still want to show 20 results, just have different text & hyperlink for records 6-20 Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 15, 2007 Share Posted November 15, 2007 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"; } } } ?> Quote Link to comment Share on other sites More sharing options...
daveh33 Posted November 15, 2007 Author Share Posted November 15, 2007 I tried that and it just displays all results as normal.. $max_results is for my paiging and there is a line of code $max_results = 5; Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 15, 2007 Share Posted November 15, 2007 Can you post more of your code. Quote Link to comment Share on other sites More sharing options...
daveh33 Posted November 15, 2007 Author Share Posted November 15, 2007 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>"; } Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 15, 2007 Share Posted November 15, 2007 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>"; ?> Quote Link to comment Share on other sites More sharing options...
daveh33 Posted November 15, 2007 Author Share Posted November 15, 2007 It still just shows the "Pick this card" link Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.