Help Needed:Random Image Links to its own page
Moderator: General Moderators
-
postmanager
- Forum Newbie
- Posts: 6
- Joined: Tue Nov 11, 2008 2:21 am
Help Needed:Random Image Links to its own page
Hello all,
I need help with a php code. I am stuck and I will really appreciate if you can help me.
I have one page that displays a random project image and project title out of a phpmyadmin database.
Code for this page:
******************************************
<?php
$result = mysql_query("SELECT id, project_title, img_bin FROM featured_projects WHERE W_C = 1 AND status = 1 ORDER BY Rand() LIMIT 1");
if (mysql_num_rows($result) == 0) { echo "<p>Sorry. We couldn`t find any projects.</p>";}
else {
while( $c = mysql_fetch_object($result)) {
$image = ($c->img_bin) ? "<img src=\"img.php?id={$c->id}&table=featured_projects\" "
."border=\"0\" alt=\"[image]\" />" : "";
printf("<div class=\"featured\">
<h3>%s</h3>
<p>$image</p>
</div>
",
$c->project_title, $c->project_title, $c->project_title, $c->project_title
);
}
}
?>
************************************************
When I click on the active image (selected randomly) in the first page, I want to go to another page and show a more detailed information about the selected project. Here is the code I write for this:
************************************************
<?php
$result = mysql_query("SELECT id, project_title, project_description , related_manager, team_members, img_bin FROM featured_projects WHERE DM = 1 AND status = 1");
if (mysql_num_rows($result) == 0) { echo "<p>Sorry. No news to report yet.</p>";}
else {
while( $c = mysql_fetch_object($result)) {
$image = ($c->img_bin) ? "<img src=\"../img.php?id={$c->id}&table=featured_projects\" "
."border=\"0\" alt=\"[image]\" />" : "";
print <<<END
<table width="690" border="0" cellpadding="0" cellspacing="20">
<tr>
<td width="550" align="left">
<h2>$c->project_title </h2><br>
<b>Manager: </b> $c->related_manager <br>
<b>Team Members: </b> $c->team_members <br>
$c->project_description <br>
</td>
<td width="140" valign="top">
$image
</td>
</tr>
</table>
END;
}
}
?>
*****************************************
My only problem is, the second code shows information about all of the projects, not the selected one.
How can I do that?
Thanks in advance,
I need help with a php code. I am stuck and I will really appreciate if you can help me.
I have one page that displays a random project image and project title out of a phpmyadmin database.
Code for this page:
******************************************
<?php
$result = mysql_query("SELECT id, project_title, img_bin FROM featured_projects WHERE W_C = 1 AND status = 1 ORDER BY Rand() LIMIT 1");
if (mysql_num_rows($result) == 0) { echo "<p>Sorry. We couldn`t find any projects.</p>";}
else {
while( $c = mysql_fetch_object($result)) {
$image = ($c->img_bin) ? "<img src=\"img.php?id={$c->id}&table=featured_projects\" "
."border=\"0\" alt=\"[image]\" />" : "";
printf("<div class=\"featured\">
<h3>%s</h3>
<p>$image</p>
</div>
",
$c->project_title, $c->project_title, $c->project_title, $c->project_title
);
}
}
?>
************************************************
When I click on the active image (selected randomly) in the first page, I want to go to another page and show a more detailed information about the selected project. Here is the code I write for this:
************************************************
<?php
$result = mysql_query("SELECT id, project_title, project_description , related_manager, team_members, img_bin FROM featured_projects WHERE DM = 1 AND status = 1");
if (mysql_num_rows($result) == 0) { echo "<p>Sorry. No news to report yet.</p>";}
else {
while( $c = mysql_fetch_object($result)) {
$image = ($c->img_bin) ? "<img src=\"../img.php?id={$c->id}&table=featured_projects\" "
."border=\"0\" alt=\"[image]\" />" : "";
print <<<END
<table width="690" border="0" cellpadding="0" cellspacing="20">
<tr>
<td width="550" align="left">
<h2>$c->project_title </h2><br>
<b>Manager: </b> $c->related_manager <br>
<b>Team Members: </b> $c->team_members <br>
$c->project_description <br>
</td>
<td width="140" valign="top">
$image
</td>
</tr>
</table>
END;
}
}
?>
*****************************************
My only problem is, the second code shows information about all of the projects, not the selected one.
How can I do that?
Thanks in advance,
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: Help Needed:Random Image Links to its own page
Did you send the project id in the "second portion" of you code because in the sql there is no mention of the id..or is DM the primary key to identify that specific project???
-
postmanager
- Forum Newbie
- Posts: 6
- Joined: Tue Nov 11, 2008 2:21 am
Re: Help Needed:Random Image Links to its own page
yes, it is my id key in my only database and it is unique for every project
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: Help Needed:Random Image Links to its own page
OK if DM is the PRIMARY KEY well it can't be if you say it's showing you all the projects since you have set DM=1 in the query!!!! Even if it were primary key then DM should be dependent in some variable, probably something sent via GET parameter..... 
-
postmanager
- Forum Newbie
- Posts: 6
- Joined: Tue Nov 11, 2008 2:21 am
Re: Help Needed:Random Image Links to its own page
Oh sorry, I misunderstood you.
My primary key named "id"
DM is not my primary key. DM is just a field in my database table.
I have many records in the table.
For this particular query, I want to select a random one from a set of pictures where DM field equals 1.
My primary key named "id"
DM is not my primary key. DM is just a field in my database table.
I have many records in the table.
For this particular query, I want to select a random one from a set of pictures where DM field equals 1.
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: Help Needed:Random Image Links to its own page
OK THEN it is really simple, you have this code
And you should send this id from the first portion of your code, probably as get parameter.
What you would want to do is:$result = mysql_query("SELECT id, project_title, project_description , related_manager, team_members, img_bin FROM featured_projects WHERE DM = 1 AND status = 1");
Code: Select all
$result = mysql_query("SELECT id, project_title, project_description , related_manager, team_members, img_bin FROM featured_projects WHERE DM = 1 AND status = 1 AND id='".$_GET['id']."'");
Re: Help Needed:Random Image Links to its own page
This is a really dangerous code! You should check all data received from users. Read about SQL Injecktion.
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: Help Needed:Random Image Links to its own page
Yes if the information is really VITAL/IMPORTANT, i suggest using bind variables.
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: Help Needed:Random Image Links to its own page
while speaking of bind variables -- for that you have to learn PDO or adodb!! I don't know php has function such as oci_bind_by_name that is available for oracle!! 