Jump to content

Recommended Posts

Hey guys, quite a noob with php, currently doing an assignment for school and would like to have links on a page and clicking each link will print dynamic content into the page based on an sql query

 

so for example, this is the way im using the code but it does not work.

 

<a href="<?php print "index.php?Link1"; ?>">Link 1</a>
<a href="<?php print "index.php?Link2"; ?>">Link 2</a>
<a href="<?php print "index.php?Link3"; ?>">Link 4</a>
<a href="<?php print "index.php?Link4"; ?>">Link 4</a>

<?php
if ($_GET['Link1'] !=NULL) {
    $liststring = "SELECT * FROM tbl_data WHERE data_color='green'";
} 
else if ($_GET['Link2'] !=NULL) {
    $liststring = "SELECT * FROM tbl_data WHERE data_color='red'";
} 
else if ($_GET['Link3'] !=NULL) {
    $liststring = "SELECT * FROM tbl_data WHERE data_color='blue'";
} 
else {
    $liststring = "SELECT * FROM tbl_data WHERE data_color='black'";
$listInfo = mysql_query($liststring);
?>

<?php
while($row = mysql_fetch_array($listInfo)) {
print "<li>".$row['data_title']." ".$row['data_date']."</li>";
}
?>

Only my else statement works as its not looking for $_GET variable

So index.php is successfully printing all fields from tbl_data where data_color is black.

But clicking any of the links results in the page just printing tbl_data where data_color is black. No errors

your if statements are not running because you dont set any value to your get variables. you just initiate them, but their values are still null. try something like

[/code]

<?php print "index.php?Link1=value"; ?>

[/code]

 

for each of your links (except for link2 its Link2=value, links 3 is Link3=value etc.)

 

 

Hope that helps!

Ok so my next question,

 

this code prints all the data in my table for title and date. I would like a loop that only prints up to the first 5 items:

<?php
while($row = mysql_fetch_array($listInfo)) {
print "<li>".$row['data_title']." ".$row['data_date']."</li>";
}
?>

 

I tried:

<?php
$i=0;
do 
{
$i++;
	while($i = mysql_fetch_array($listInfo)) {
	print "<li>".$i['data_title']." ".$i['data_date']."</li>";
	}
}
while ($i<5);
?>

and

<?php
$i=0;
do 
     {
     $i++;
     print "<li>".$row['data_title']." ".$row['data_date']."</li>";
     }
while 
     {
     ($row = mysql_fetch_array($listInfo)<$i)
     }
?>

 

Not that I expected either of those to work but just to show you an idea of what I'm trying to achieve. I;m not sure if I need a for loop or while loop or what.

two things you could do. First, in your actual SQL query, you could use the limit command like so:

$sql = mysql_query("your query stuff... LIMIT 5");

that would limit the returned results to 5 rows.

 

OR you could use the break command. Something like

[/code]

<?php

$i = 0;

while($row = mysql_fetch_array($listInfo)) {

if ($i == 5){

break;

}

print "<li>".$row['data_title']." ".$row['data_date']."</li>";

$i++;

}

?>

[/code]

 

the break command just tells the code to leave the loop at that point.

 

Hope that helps!

ahh perfect again! that break is interesting its like half a loop function and half a switch statement from AS3.

 

So what I want to know next (I'm not asking for code just a concept/explanation) is how hard/doable is code that will print the first 5 items from my table and then print the next 5 in a separate location such as a second page.

 

I guess an example would be a forum:

Page one print 50 topics. Once 50 have been printed, create second page and print next 50, and so on.

Or maybe:

Page one print topics 0 to 49. Page 2 print topics 50 to 100 and so on.

 

I'm assuming I can no longer print into the same page to achieve this?

Not entirely difficult, but it will take a bit of coding. Again, you can use the Limit command in SQL, but instead of using one number, use two. IE for the first fifty, you do something like

 

mysql_query("YOUR QUERY... LIMIT 50");

 

for the second page do something like

mysql_query("YOUR QUERY... LIMIT 51, 100");

 

I'm pretty sure thats the proper syntax, but You may want to double check on that.

 

You will also probably want to have some variables that change depending on the current page, so you dont have to write 50 different pages all with the same query slightly changed. As in

$start = 0
$end = 50;
if (isset($page)){
$start = end+1;
$end *= page;
}
$query = mysql_query("YOUR QUERY limit $start, $end");

 

again you may want to check my syntax. You are going to definitely want to change the above code around the fit to your needs of course, but something similar to that would probably do it. Although you may want to check with someone more versed in doing queries like that tho, because my experience with them is limited.

 

Hope that helped!

Ok, I'm going to try what you're doing but my problem is that I don't think I can use the 'LIMIT' because your example of using the LIMIT feature is in the sql query, which is in my code here:

 

<?php $listInfo = mysql_query($liststring); ?>

 

as you can see I'm assigning my query to a variable and then using the variable later such as the example loop you gave me. $liststring is my query you can see in my original post.

 

<?php
$i = 0;
while($row = mysql_fetch_array($listInfo)) {
if ($i == 5){
break;
}
print "<li>".$row['data_title']." ".$row['data_date']."</li>";
$i++;
}
?>

 

I have tried but don't think I can use the LIMIT like this:

<?php while($row = mysql_fetch_array($listInfo."LIMIT 50".)) ?>

Is it possible to put the LIMIT in my while statement so I don't need to use that loop and then I can attempt this print a certain # of items per page

 

 

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.