/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP Help with parameter…

My page new_items.php displays all the contents from the ‘items’ table of my database, however I want to display solely the data where type equals 1 when the url is new_items.php?type=1
My current code :

[code=php]<?
require_once (‘../db2connect.php’);

// Number of records to show per page:
$display = 8;

$type = (isset($_GET[‘type’])) ? $_GET[‘type’] : “%”;

$genre = (isset($_GET[‘genre’])) ? $_GET[‘genre’] : “%”;

// Determine how many pages there are.
if (isset($_GET[‘np’])) { // Already been determined.

$num_pages = $_GET[‘np’];

} else { // Need to determine.

// Count the number of records
$query = “SELECT COUNT(i.id) FROM gitems as i ORDER BY i.id ASC”;
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];

// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}

} // End of np IF.

// Determine where in the database to start returning results.
if (isset($_GET[‘s’])) {
$start = $_GET[‘s’];
} else {
$start = 0;
}

?>
<p class=”maintext”>We are constantly adding new items to our web site and here
is where you’ll find them first. <br />Please take a look around items adding in the
last month.</p>
<center>
<form name=”jump”>
<select name=”type” onChange=”location=document.jump.type.options[document.jump.type.selectedIndex].value;” value=”GO”>
<option value=”new_items.php”>Select…</option>
<?
require_once (‘../mysql_connect.php’); // Connect to the database
$query = “SELECT type, id FROM types ORDER BY id”;
$result = mysql_query ($query); // Run the Query
while ($row = mysql_fetch_array($result)) {
echo ‘<option value=”new_items.php?type=’ . $row[‘id’] . ‘”>’ . $row[‘type’] . ‘</option>’;
}
?>

</select>
</form>
</center>
<?

if (!isset($type) || empty($type)) {
$query2 = “SELECT i.id, i.name, i.types, i.descr, i.thumb, i.price, t.id, t.type, p.id, p.name FROM gitems as i, publishers as p, types as t WHERE i.publisher = p.id AND i.types = t.id ORDER BY i.id DESC LIMIT $start, $display”;
$result2 = mysql_query($query2) or die(mysql_error());
}
else
{
$query2 = “SELECT i.id, i.name, i.types, i.descr, i.thumb, i.price, t.id, t.type, p.id, p.name FROM gitems as i, publishers as p, types as t WHERE i.publisher = p.id AND i.types = t.id AND i.types = ‘$type’ ORDER BY i.id DESC LIMIT $start, $display”;
}

$result2 = mysql_query($query2) or die(mysql_error());

echo ‘<table width=”100%” border=”0″ class=”maintext” cellpadding=”2″>’;

while ($row2 = mysql_fetch_array($result2)) {

echo ‘<tr>
<td width=”250″ valign=”top”>Title : <b><a href=”item.php?id=’ . $row2[0] . ‘”>’ . $row2[1] . ‘</a></b>(‘ . $row2[7] . ‘)<br /><br />Description : ‘ .substr($row2[3],0,140). ‘…</td>
<td><a href=”item.php?id=’ . $row2[0] . ‘”><img src=”CrashBandicoot.jpg” width=”80″ height=”80″ border=”3″ style=”border-color:#cccccc” hspace=”5″></a></td>
</tr>
<tr>
<td width=”250″></td>
<td valign=”top”>Price : £’ . $row2[5] . ‘ GBP</td>
</tr>
<tr>
<td width=”250″><a href=”item.php?id=’ . $row2[0] . ‘”>Further Information</a></td>
<td><a href=”add_cart.php?item=’ . $row2[0] . ‘”><img src=”add_item.jpg” border=”0″ alt=”Add ‘ . $row2[1] . ‘ to your cart!” /></a></td>
</tr>
<tr>
<td colspan=”2″><hr size=1 color=#00CCFF /></td>
</tr>’;

}

echo ‘</table>’;

// Make the links to other pages, if necessary.
if ($num_pages > 1) {

echo ‘<br /><p class=”maintext”>’;
// Determine what page the script is on.
$current_page = ($start/$display) + 1;

// If it’s not the first page, make a Previous button.
if ($current_page != 1) {
echo ‘<a href=”new_items.php?s=’ . ($start – $display) . ‘&np=’ . $num_pages . ‘”>Previous</a> ‘;
}

// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo ‘<a href=”new_items.php?s=’ . (($display * ($i – 1))) . ‘&np=’ . $num_pages . ‘”>’ . $i . ‘</a> ‘;
} else {
echo $i . ‘ ‘;
}
}

// If it’s not the last page, make a Next button.
if ($current_page != $num_pages) {
echo ‘<a href=”new_items.php?s=’ . ($start + $display) . ‘&np=’ . $num_pages . ‘”>Next</a>’;
}

echo ‘</p>’;

} // End of links section.

?>
[/code]

Any ideas ?

Thankyou,,

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@invisionauthorSep 11.2006 — wrong post
Copy linkTweet thisAlerts:
@NogDogSep 11.2006 — [code=php]
} else { // Need to determine.

$from = "";
$where = "WHERE ";

$from.= ", types as t";
$where.= "i.types = t.id AND t.type = '$type' AND DATE_SUB(CURDATE(),INTERVAL 30 DAY)";

$where = ($where == "WHERE ") ? "" : $where;
[/code]
[/quote]

Not sure what is intended here, but in the last line of this segment the value of $where will never be "WHERE", because you're setting it explicitly in the preceding line.
Copy linkTweet thisAlerts:
@invisionauthorSep 11.2006 — Can someone please take a look at the code again.

1) I just want to display all the items if a type is not stipulated.

2) If a type is stipulated, then it should display the items relevant to that type. < I can do this bit, but not 1).

Please help.
Copy linkTweet thisAlerts:
@invisionauthorSep 11.2006 — its ok, it works now ?
×

Success!

Help @invision spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...