Join 2 tables by merk_id - php

I'm trying to combine to tables. I got 11 different brands and I got wheels that belong to a brand. You can select a brand by its name with an option selectbox and when submut I want alle the wheels with that brand.
This is the class I'm using:
<?php
include_once "connect.class.php";
class merken extends connect
{
private $merkenlijst;
public function getMerken($database, $id = NULL)
{
$sql = "SELECT * FROM ".$database."_merken";
if(!empty($id))
{
$sql .= " WHERE merk_code=:id";
}
else
{
$sql .= " ORDER BY merk_naam ASC";
}
try
{
$stmt = $this->db->prepare($sql);
if(!empty($id)){ $stmt->bindParam(":id", $id, PDO::PARAM_STR); }
$stmt->execute();
$this->merkenlijst = $stmt->fetchAll(PDO::FETCH_OBJ);
$stmt->closeCursor();
return $this->merkenlijst;
}
catch (Exception $e)
{
die ( $e->getMessage() );
}
}
public function __construct($dbo = NULL)
{
parent::__construct($dbo);
}
}
?>
This is how I echo the info from my database:
<div class="bandwielkolom">
<form action="index.php?lang=nl&p=<?php echo $_GET['p']; ?>#keuze" method="post">
<table class="bandentabel">
<tr>
<th colspan="2">Zoek op merk<a name="wiel"></a></th>
</tr>
<tr>
<td>Merk:</td>
<td>
<select name="wiel_merk">
<option value="0">- Merk -</option>
<?php
$merken = $merkclass->getMerken($website);
foreach($merken as $merk)
{
echo "\t\t\t\t\t\t\t\t\t\t\t<option value=\"".$merk->merk_code."\"";
if(isset($_GET['search']) && $_GET['search'] == "wiel" && isset($_GET['merk']) && $_GET['merk'] == $merk->merk_code || isset($_POST['wiel_submit']) && $_POST['wiel_merk'] == $merk->merk_code) { echo " selected=\"selected\""; }
echo ">".$merk->merk_naam."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="wiel_submit" value="Zoek" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
</div>
<div class="clearboth"></div>
<br />
<?php
if(isset($_POST['wiel_submit']) && $_POST['wiel_submit'] == "Zoek" || isset($_GET['merk']))
{
$merk = NULL;
if(isset($_POST['wiel_submit']) && $_POST['wiel_submit'] == "Zoek")
{
$merk = $_POST['wiel_merk'];
}
$merken = $merkclass->getMerken($website, $merk);
foreach($merken as $merk)
{
?>
<img src="http://www.etyre.net/preview/bnet/logos/<?php echo str_replace(".png", "_150.png", $merk->merk_logo); ?>" width="150" class="logo" alt="<?php echo $merk->merk_naam; ?>"/>
<div id="merken">
<li><span class="title"><?php echo $merk->wiel_info; ?></span>
<a href="http://www.inter-tyre.nl/inter-tyre/images/w3/<?php echo $merk->wiel_foto; ?>" class="preview" title="Fotonummer: <?php echo $merk->wiel_foto; ?>">
<img src="http://www.inter-tyre.nl/inter-tyre/images/w2/<?php echo $merk->wiel_foto; ?>" alt="Fotonummer: <?php echo $merk->wiel_foto; ?>" class="wheelImg"/>
</a>
<div class="clearboth"></div>
</div>
<?php
}
?>
<?php
}
?>
How can I join this two tables together so than when I select a brand it does show all the wheels that has that brand? And no double brands in my select option?
wielen table
merken table
Can somebody help me out?
Thanks

Your question is a little unclear, and it appears to me that you are too lazy to just go and read the documentation for JOIN.
Anyway, something along the following lines should work:
SELECT w.*,m.* FROM wielen w
LEFT JOIN merken m ON w.merk_id = m.id
WHERE m.id = $your_current_merk;
This gives you a list of all wheels, together with their associated brand information.
If you want to do it the other way around and you don't want to have your brands reoccurring, with your current setup, you would want to do a GROUP_CONCAT, like in this question.

Related

Very heavy wordpress plugin and customization

i have a wordpress website and with customization (cache/minify/database), is really quick in frontend, and in backend, with one exception. I use a plugin where it stores and retrieves players stats, carreers, teams etc.
When saving the team's match player's statistics, it needs 5 minutes to show the page reloaded with the data.
Also when retrieving for example team's season data, it also needs a lot of time to show them.
From what i ve seen, it is related with two of my files that do all the work. tournament_match.php, and functions.php. From hosting they said that it gets all tha database and thats why it is so slow
Here is the tournament_match file
<?php
global $wpdb;
global $msg;
$page = $_GET['page'];
$tournament_id = $_GET['tid'];
$match_id = $_GET['mid'];
$league_type = leagueengine_fetch_data_from_id($tournament_id,'league_type');
if(isset($_POST['home_team_bonus']) or isset($_POST['away_team_bonus'])) {
if(isset($_POST['save_tournament_match'])) { leagueengine_save_tournament_match($tournament_id,$match_id,$_POST['date_alt'],$_POST['time_alt'],$_POST['home_team_id'],$_POST['away_team_id'],$_POST['home_team_score'],$_POST['away_team_score'],$_POST['home_team_bonus'],$_POST['away_team_bonus']); }
} else {
if(isset($_POST['save_tournament_match'])) { leagueengine_save_tournament_match($tournament_id,$match_id,$_POST['date_alt'],$_POST['time_alt'],$_POST['home_team_id'],$_POST['away_team_id'],$_POST['home_team_score'],$_POST['away_team_score']); }
}
if(isset($_POST['save_attributes'])) { leagueengine_save_attribute_values('tournament_match',NULL,NULL,$match_id,NULL,NULL,$tournament_id); }
if(isset($_POST['add_event_to_match'])) { error_reporting(0); leagueengine_add_event_to_tournament_match($tournament_id,$match_id,$_POST['new_event_id'],$_POST['new_event_time'],$_POST['timeline_text'],$_POST['new_event_count'],$_POST['new_event_player_id']); error_reporting(1); }
if(isset($_POST['save_events'])) { leagueengine_save_event_times('tournament_match',$_POST['event_time_id'],$_POST['event_time'],$_POST['event_text']); }
if(isset($_POST['add_home_event_to_match'])){
error_reporting(0);
foreach($_POST as $key=>$val){
$val=intval($val);
if(is_int($val)&&$val>0){
$data=explode('-',$key);
$playerid=$data[1];
$eventid=$data[2];
leagueengine_add_event_to_tournament_match2($tournament_id,$match_id,$eventid,$_POST['new_event_time'],$_POST['timeline_text'],$val,$playerid);
}
}
error_reporting(1);
}
if(isset($_POST['add_away_event_to_match'])){
error_reporting(0);
foreach($_POST as $key=>$val){
$val=intval($val);
if(is_int($val)&&$val>0){
$data=explode('-',$key);
$playerid=$data[1];
$eventid=$data[2];
leagueengine_add_event_to_tournament_match2($tournament_id,$match_id,$eventid,$_POST['new_event_time'],$_POST['timeline_text'],$val,$playerid);
}
}
error_reporting(1);
}
if(isset($_POST['save_tournament_match_lineups'])) {
if(isset($_POST['homeplayers'])) { $homeplayers = $_POST['homeplayers']; } else { $homeplayers = ''; }
if(isset($_POST['awayplayers'])) { $awayplayers = $_POST['awayplayers']; } else { $awayplayers = ''; }
if(isset($_POST['homesubs'])) { $homesubs = $_POST['homesubs']; } else { $homesubs = ''; }
if(isset($_POST['awaysubs'])) { $awaysubs = $_POST['awaysubs']; } else { $awaysubs = ''; }
leagueengine_save_tournament_match_lineups($tournament_id,$match_id,$homeplayers,$awayplayers,$homesubs,$awaysubs);
}
if(isset($_POST['delete_events'])) { leagueengine_delete_data('tournament_match_event',$_POST['delete_id'],'tournament',NULL,NULL,$tournament_id,$match_id); }
if(isset($_POST['save_match_statistics'])) { leagueengine_save_tournament_match_statistics($tournament_id,$match_id,$_POST['tournament_match_statistic'],$_POST['home_value'],$_POST['away_value'],$_POST['att_type']); }
if(isset($_POST['save_tournament_match_preview'])) { leagueengine_save_tournament_match_preview($tournament_id,$match_id,stripslashes_deep($_POST['match_preview'])); }
if(isset($_POST['save_tournament_match_report'])) { leagueengine_save_tournament_match_report($tournament_id,$match_id,stripslashes_deep($_POST['match_report'])); }
if(isset($_POST['tournament_match_swap'])) { leagueengine_tournament_match_swap($tournament_id,$match_id); }
$table = $wpdb->prefix . 'leagueengine_tournament_matches';
$match = $wpdb->get_row("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND id = '$match_id'");
$home_team_id = $match->home_team_id;
$away_team_id = $match->away_team_id;
$tournament = leagueengine_fetch_data_row('tournament',$tournament_id);
$table2 = $wpdb->prefix . 'leagueengine_tournaments';
$tournament_row = $wpdb->get_row("SELECT * FROM $table2 WHERE data_id = '$tournament_id'");
?>
<div id="leagueengine_admin" class="<?php echo $page; ?>">
<?php echo leagueengine_admin_header(); ?>
<div id="leagueengine_admin_content">
<?php if($msg) { echo $msg; } ?>
<ul class="breadcrumbs">
<li><?php _e('Competitions','leagueengine');?> <span class="divider">/</span></li>
<li><?php echo leagueengine_fetch_data_from_id($tournament_id,'data_value') ?> <span class="divider">/</span></li>
<?php if($match->round == 'GROUP') { ?>
<li><?php _e('Groups','leagueengine');?> <span class="divider">/</span></li>
<?php } else { ?>
<li><?php _e('Knockout','leagueengine');?> <span class="divider">/</span></li>
<?php } ?>
<li><?php _e('Match','leagueengine');?></li>
</ul>
<?php
if($league_type == 'players') {
$home_emblem = leagueengine_fetch_player_emblem($match->home_team_id,20);
$away_emblem = leagueengine_fetch_player_emblem($match->away_team_id,20,'right');
} else {
$home_emblem = leagueengine_fetch_team_emblem($match->home_team_id,20);
$away_emblem = leagueengine_fetch_team_emblem($match->away_team_id,20,'right');
}
?>
<div class="match_masthead">
<table>
<tr>
<td class="home_team" style="border-top: 5px solid <?php echo leagueengine_fetch_team_colour($match->home_team_id,'primary');?>; text-align:left;width:40%;"><?php echo $home_emblem . leagueengine_fetch_data_from_id($match->home_team_id,'data_value');?></td>
<td class="score" style="text-align:center;width:20%;"><span><?php echo $match->home_team_score;?> &dash; <?php echo $match->away_team_score;?></span></td>
<td class="away_team" style="border-top: 5px solid <?php echo leagueengine_fetch_team_colour($match->away_team_id,'primary');?>; text-align:right;width:40%;"><?php echo leagueengine_fetch_data_from_id($match->away_team_id,'data_value') . $away_emblem;?></td>
</tr>
<tr>
<td colspan="3" style="text-align:center;"><?php echo date(leagueengine_fetch_settings('date_format_php'),strtotime($match->match_date)) . ' ' . date(leagueengine_fetch_settings('time_format_php'),strtotime($match->match_time)); ?></td>
</tr>
<tr>
<td class="competition" colspan="3" style="text-align:center;"><?php echo '' . leagueengine_fetch_data_from_id($tournament_id,'data_value') . ''; ?></td>
</tr>
<tr><td colspan="100%" style="text-align:center;padding-bottom:20px;"><?php echo leagueengine_link('tournament_match&tid='.$tournament_id.'&mid='.$match->id,__('Go To Match','leagueengine'),'','','button-primary'); ?></td></tr>
</table>
</div>
<div id="leagueengine_tabs">
<ul>
<li><?php _e('Score','leagueengine');?></li>
<?php
$table = $wpdb->prefix . 'leagueengine_player_careers';
$homeplayers = $wpdb->get_results("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND team_id = '$home_team_id'");
$awayplayers = $wpdb->get_results("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND team_id = '$away_team_id'");
if($homeplayers or $awayplayers && $league_type != 'players') {
?>
<li><?php _e('Lineups','leagueengine');?></li>
<?php } ?>
<?php if(leagueengine_data_exists('event')) { echo '<li>' . __('Events','leagueengine') . '</li>'; } ?>
<?php if(leagueengine_statistics_exists('tournament_match')) { echo '<li>' . __('Statistics','leagueengine') . '</li>'; } ?>
<?php if(leagueengine_h2h_history($match->match_date,$home_team_id,$away_team_id)) { echo '<li>' . __('History','leagueengine') . '</li>'; } ?>
<li><?php _e('Report','leagueengine');?></li>
</ul>
<div id="score">
<form action="" method="POST">
<table class="form">
<tr>
<th style="width:20%;"><?php _e('Date/Time','leagueengine');?></th>
<th style="width:30%;text-align:center;"><?php _e('Home','leagueengine');?></th>
<th style="width:20%;text-align:center;"><?php _e('Score','leagueengine');?></th>
<th style="width:30%;text-align:center;"><?php _e('Away','leagueengine');?></th>
</tr>
<tr class="date">
<td><input type="text" class="leagueengine_datepicker" name="match_date" value="<?php echo date(leagueengine_fetch_settings('date_format_php'),strtotime($match->match_date));?>"></td>
<td colspan="3"></td>
</tr>
<input type="hidden" name="tournament_match_id" value="<?php echo $match->id;?>">
<input type="hidden" name="home_team_id" value="<?php echo $match->home_team_id;?>">
<input type="hidden" name="away_team_id" value="<?php echo $match->away_team_id;?>">
<input type="hidden" name="date_alt" class="leagueengine_datepicker_alt" value="<?php echo $match->match_date;?>">
<input type="hidden" name="time_alt" class="leagueengine_timepicker_alt" value="<?php echo $match->match_time;?>">
<tr>
<td><input type="text" class="leagueengine_timepicker" name="match_time" value="<?php echo date(leagueengine_fetch_settings('time_format_php'),strtotime($match->match_time));?>"></td>
<td style="text-align:center;"><?php echo leagueengine_fetch_data_from_id($match->home_team_id,'data_value') ;?></td>
<td style="text-align:center;">
<input style="width:48%;text-align:center;" type="text" name="home_team_score" value="<?php echo $match->home_team_score;?>">
<input style="width:48%;text-align:center;" type="text" name="away_team_score" value="<?php echo $match->away_team_score;?>">
</td>
<td style="text-align:center;"><?php echo leagueengine_fetch_data_from_id($match->away_team_id,'data_value') ;?></td>
</tr>
<?php if($tournament_row->pts_bonus == 'on') { ?>
<tr>
<td></td>
<td style="text-align:center;"><?php _e('Bonus Points','leagueengine');?></td>
<td style="text-align:center;">
<input style="width:48%;text-align:center;" type="text" name="home_team_bonus" value="<?php echo $match->home_team_bonus;?>">
<input style="width:48%;text-align:center;" type="text" name="away_team_bonus" value="<?php echo $match->away_team_bonus;?>">
</td>
<td style="text-align:center;"><?php _e('Bonus Points','leagueengine');?></td>
</tr>
<?php } ?>
</table>
<input style="margin-top:20px;" type="submit" name="save_tournament_match" class="button-primary" value="<?php _e('Save','leagueengine');?>" />
<input style="margin:20px 0 0 10px;" type="submit" name="tournament_match_swap" class="button" value="<?php _e('Swap Teams','leagueengine');?>" style="float:right;margin-right:10px;" />
</form>
</div>
<?php
$table = $wpdb->prefix . 'leagueengine_player_careers';
$homeplayers = $wpdb->get_results("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND team_id = '$home_team_id'");
$awayplayers = $wpdb->get_results("SELECT * FROM $table WHERE tournament_id = '$tournament_id' AND team_id = '$away_team_id'");
if($homeplayers or $awayplayers && $league_type != 'players') {
?>
<div id="lineups">
<?php echo leagueengine_tournament_match_lineups($tournament_id,$match_id);?>
</div>
<?php } ?>
<?php if(leagueengine_data_exists('event')) { ?>
<div id="events">
<?php echo leagueengine_tournament_match_events($tournament_id,$match_id);?>
</div>
<?php } ?>
<?php if(leagueengine_statistics_exists('tournament_match')) { ?>
<div id="statistics">
<?php echo leagueengine_fetch_statistics('tournament_match',NULL,NULL,$tournament_id,$match_id);?>
</div>
<?php } ?>
<?php if(leagueengine_h2h_history($match->match_date,$home_team_id,$away_team_id)) { ?>
<div id="history">
<?php echo leagueengine_fetch_h2h_history($match->match_date,$home_team_id,$away_team_id);?>
</div>
<?php } ?>
<div id="report">
<form action="" method="post">
<div class="setting">
<table class="form">
<tr><th><?php _e('Match Report','leagueengine');?></th></tr>
</table>
<?php wp_editor( stripslashes_deep($match->report), 'match_report', array( 'media_buttons' => true, 'tinymce' => true, 'quicktags' => true, 'textarea_rows' => 20 )); ?>
</div>
<input style="margin-top:20px;" type="submit" name="save_tournament_match_report" class="button-primary" value="<?php _e('Save','leagueengine'); ?>">
</form>
</div>
</div>
</div>
</div>
</div>

Multiple Checkboxes Columns Updating Multiple SQL Rows

I've never had issues programming PHP SQL updates when updating multiple rows of data with multiple columns, but for some reason this section of coding is killing me as it is nothing more than checkboxes for the end user to check and/or uncheck. I've include portions of the code below as the full code is over 50+ columns wide of checkboxes and can range from 1-200 rows.
Everything displays/runs the way its supposed to but the updating isnt functioning properly ... example: if there is 5 rows (each with a unique trackingid) and I check the checkbox for the 4th row on update it says that the 1st checkbox was checked and NOT the 4th row.
<?php if (!isset($_POST['trackingid'])) { ?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" enctype="multipart/form-data" class="form-horizontal" id="form"><input type="hidden" name="id" value="<?php echo $id; ?>">
<table class="table table-striped">
<thead>
<tr>
<td class="rotate-alt"></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt2"><div><span></span></div></td>
<td class="rotate-alt"><div><span>Glass</span></div></td>
<td class="rotate-alt"><div><span>Glass - SDL</span></div></td>
<td class="rotate-alt"><div><span>Mill - S&R</span></div></td>
</tr>
</thead>
<tbody>
<?php $result = sqlsrv_query($conn, "SELECT id, [Order Number], [Door Number], [Qty Display], [Interior or Exterior], [Style], Species_DD_Desc, [Mill Batch Nbr],
[Glass Completed], [Glass Completed By], [Glass STD Hrs], [SDL Completed], [SDL Completed By], [SDL STD Hrs],
[Mill Completed], [Mill Completed By], [Mill STD Hrs]
FROM PD_Tracking
WHERE [Order Number] = '$id'
ORDER BY [Door Number], [Qty Display], [Mill Batch Nbr]");
$i=0;
while ($list = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){ ?>
<tr>
<td class="center">
<input type="checkbox" onclick="$('input[id=\'selid<?php echo $i; ?>\']').attr('checked', this.checked);">
<input type="hidden" name="trackingid[]" value="<?php echo $list['id']; ?>">
</td>
<td class="center"><?php echo $list['Door Number']; ?></td>
<td class="center"><?php echo $list['Qty Display']; ?></td>
<td class="center"><?php echo $list['Interior or Exterior']; ?></td>
<td class="center"><?php echo $list['Style']; ?></td>
<td class="center"><?php echo $list['Species_DD_Desc']; ?></td>
<td class="center">
<?php if (($list['Glass STD Hrs']>0) && (!isset($list['Glass Completed']))){ ?>
<input type="checkbox" name="glass[]" id="selid<?php echo $i; ?>">
<?php } else if (($list['Glass STD Hrs']>0) && ($list['Glass Completed'] > ' ')){ ?>
<input type="checkbox" name="glass[]" checked="checked" id="selid<?php echo $i; ?>">
<?php } else { ?>
NR
<?php } ?>
</td>
<td class="center">
<?php if (($list['SDL STD Hrs']>0) && (!isset($list['SDL Completed']))){ ?>
<input type="checkbox" name="sdl[]" id="selid<?php echo $i; ?>">
<?php } else if (($list['SDL STD Hrs']>0) && ($list['SDL Completed'] > ' ')){ ?>
<input type="checkbox" name="sdl[]" checked="checked" id="selid<?php echo $i; ?>">
<?php } else { ?>
NR
<?php } ?>
</td>
<td class="center">
<?php if (($list['Mill STD Hrs']>0) && (!isset($list['Mill Completed']))){ ?>
<input type="checkbox" name="mill[]" id="selid<?php echo $i; ?>">
<?php } else if (($list['Mill STD Hrs']>0) && ($list['Mill Completed'] > ' ')){ ?>
<input type="checkbox" name="mill[]" checked="checked" id="selid<?php echo $i; ?>">
<?php } else { ?>
NR
<?php } ?>
</td>
</tr>
<?php ++$i;
} ?>
</tbody>
</table>
<?php } // END if (!isset($_POST['trackingid'])) {
if (isset($_POST['trackingid'])) {
$query = sqlsrv_query($conn, "SELECT * FROM users WHERE id=".$_SESSION['auid']."");
$row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);
$employee_num = $row['Employee Number'];
$size = count($_POST['trackingid']);
for($i=0; $i < $size; $i++){
$trackingid = $_POST['trackingid'][$i];
$query = "UPDATE PD_Tracking SET ";
if(isset($_POST['glass'][$i])){
$query .= "[Glass Completed]='$date_time_entered', [Glass Completed By]='$employee_num', ";
} else {
$query .= "[Glass Completed]= NULL, [Glass Completed By]= NULL, ";
}
if(isset($_POST['sdl'][$i])){
$query .= "[SDL Completed]='$date_time_entered', [SDL Completed By]='$employee_num', ";
} else {
$query .= "[SDL Completed]= NULL, [SDL Completed By]= NULL, ";
}
if(isset($_POST['mill'][$i])){
$query .= "[Mill Completed]='$date_time_entered', [Mill Completed By]='$employee_num' ";
} else {
$query .= "[Mill Completed]= NULL, [Mill Completed By]= NULL ";
}
$query .= "WHERE id='$trackingid'";
$query = sqlsrv_query($conn, $query);
} // END for($i=0; $i < $size; $i++){?>
<SCRIPT LANGUAGE='JavaScript'>
<!-- Begin
window.location = '<?php echo $_SERVER['REQUEST_URI']; ?>&success=edit';
// End -->
</script>
<?php } // END if (isset($_POST['trackingid'])) { ?>

Missing the result printed from database

My purpose is search book type from input keyboard and count it. I have error: counting the number of book is right but when print, it miss some result. For example, I put 'f' it show 7 result( right) however it print only 2 two result. Here is my code:
<form action="" method="POST">
Enter type of book here:<input type="text" size="20" name="sbt"> <br />
<input type="submit" name="sb" value="Search">
</form>
<table align="center" border="1" width="600">
<thead><tr align="center">
<tr align="center">
<td><b>Book ID</b></td>
<td><b>Book Title</b></td>
<td><b>Book Author</b></td>
<td><b>Pulished Year</b></td>
<td><b>Book Type</b></td>
<td><b>Status</b></td>
</tr>
<?php
if (isset($_POST['sb'])) {
$s="";
if ($_POST['sbt'] == null) {
echo "Please re-enter <br>";
} else
{
$s = $_POST['sbt'];
}
$q = "SELECT * FROM book WHERE book_type LIKE '%$s%' ";
$r= mysqli_query($conn,$q);
while($row = mysqli_fetch_array($r)) {
?>
<tr align="center">
<td><?php echo $row['book_no'];?></td>
<td><?php echo $row['book_title'];?></td>
<td><?php echo $row['book_author'];?></td>
<td><?php echo $row['book_year'];?></td>
<td><?php echo $row['book_type'];?></td>
<td>
<?php
if ($row['book_quantity'] == 1) {
echo "Available";
}
else {
echo "Not available";
}
?>
</td>
<?php
$c=" SELECT COUNT(DISTINCT(book_no)) AS totaltype FROM book WHERE book_type LIKE '%$s%'";
$s= mysqli_query($conn, $c);
if (mysqli_num_rows($s)> 0 )
{
$row2= mysqli_fetch_array($s);
echo " Total book type result:"."{$row2['totaltype']}";
}
}
?>
<?php } ?>
</table>
Remove "%" before $s and try.
for example:
$query = mysql_query("SELECT * FROM table WHERE the_number LIKE '$yourPHPVAR%'");

Showing data from category with php mysql

My tables are:
barangtbl: id, judul_barang, judul_seo, keywords, deskripsi, id_kat, id_sub, id_supersub, kategori_seo, view, gambar
kategori: id_kat, nama_kat
subkategori: id_sub, id_kat, nama_sub
supersubkategori: id_supersub, id_sub, id_kat, nama_supersub
I have a problem with showing data in category from database with PHP, the problem is when i click link: localhost/test/category.php?name=HPI, it doesn't show any data, but if I change HPI with number: 15, it show all.
15 is id_supersub data on supersubkategori table where I join with barangtbl table. So, all i want is if someone click: localhost/test/category.php?name=HPI it will show data with HPI category inside. How solve this problem?
<?php
if (isset($_GET['name']))
{
$kategori = $_GET['name'];
}
include "config.php";
if ((isset($kategori)) =='')
{
$query = "SELECT * FROM barangtbl INNER JOIN supersubkategori on supersubkategori.id_supersub = barangtbl.id_supersub ORDER BY id DESC LIMIT 0,12";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
else
{
echo "
<table width=\"100%\">
<tr>
<td align=\"center\"><b><font color=\"red\" size=\"2.5\">[ ".$_GET['name']." ]</b></font></td>
</tr>
</table>";
$query = "SELECT * FROM barangtbl WHERE id_supersub = '$kategori' ORDER BY id";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
?>
<table cellpadding="10" cellspacing="2" align="center">
<tr>
<?php
$kolom=3;
$x = 0;
if($numrows > 0)
{
while($data=mysql_fetch_array($hasil))
{
if ($x >= $kolom)
{
echo "</tr><tr>";
$x = 0;
}
$x++;
?>
<th>
<div id="title">
<a href="product.php?id=<?php echo $data['id']; ?>">
<?php echo $data['judul_barang']; ?>
</a>
<br><br>
</div>
<div id="image">
<a href="product.php?id=<?php echo $data['id']; ?>">
<img width='150' height='150' valign='top' border='1,5' src="product/<?php echo $data['gambar']; ?>" />
</a>
<br><br>
</div>
<div id="action">
<?php
echo '
<a href="product.php?id='.$data['id'].'">
<img src="images/detail.jpg"\ title="Detail Barang" border="0" width=\"50\" height=\"30\">
</a>';
?>
</div>
<hr />
</th>
<?php
}
}
?>
</tr>
</table>
Try removing the quotes
$query = "SELECT * FROM barangtbl WHERE id_supersub = $kategori ORDER BY id";

Sort By Date last created and limit to five result

I am trying to do two things one is to Sort By Date last created the next one is to limit the the amount of data show to five (5) so ticket one, ticket two, ticket three, four, and five
I am making a drop down window that shows the last 5 ticket that was created and i am getting the information from below
Thank you for your help
<?php
if(!defined('OSTCLIENTINC') || !is_object($thisclient) || !$thisclient->isValid()) die('Access Denied');
$qstr='&'; //Query string collector
$status=null;
if(isset($_REQUEST['status'])) { //Query string status has nothing to do with the real status used below.
$qstr.='status='.urlencode($_REQUEST['status']);
//Status we are actually going to use on the query...making sure it is clean!
switch(strtolower($_REQUEST['status'])) {
case 'open':
case 'closed':
$status=strtolower($_REQUEST['status']);
break;
default:
$status=''; //ignore
}
} elseif($thisclient->getNumOpenTickets()) {
$status='open'; //Defaulting to open
}
$sortOptions=array('id'=>'`number`', 'subject'=>'subject.value',
'status'=>'ticket.status', 'dept'=>'dept_name','date'=>'ticket.created');
$orderWays=array('DESC'=>'DESC','ASC'=>'ASC');
//Sorting options...
$order_by=$order=null;
$sort=($_REQUEST['sort'] && $sortOptions[strtolower($_REQUEST['sort'])])?strtolower($_REQUEST['sort']):'date';
if($sort && $sortOptions[$sort])
$order_by =$sortOptions[$sort];
$order_by=$order_by?$order_by:'ticket_created';
if($_REQUEST['order'] && $orderWays[strtoupper($_REQUEST['order'])])
$order=$orderWays[strtoupper($_REQUEST['order'])];
$order=$order?$order:'ASC';
if($order_by && strpos($order_by,','))
$order_by=str_replace(','," $order,",$order_by);
$x=$sort.'_sort';
$$x=' class="'.strtolower($order).'" ';
$qselect='SELECT ticket.ticket_id,ticket.`number`,ticket.dept_id,isanswered, '
.'dept.ispublic, subject.value as subject,'
.'dept_name,ticket. status, ticket.source, ticket.created ';
$dynfields='(SELECT entry.object_id, value FROM '.FORM_ANSWER_TABLE.' ans '.
'LEFT JOIN '.FORM_ENTRY_TABLE.' entry ON entry.id=ans.entry_id '.
'LEFT JOIN '.FORM_FIELD_TABLE.' field ON field.id=ans.field_id '.
'WHERE field.name = "%1$s" AND entry.object_type="T")';
$subject_sql = sprintf($dynfields, 'subject');
$qfrom='FROM '.TICKET_TABLE.' ticket '
.' LEFT JOIN '.DEPT_TABLE.' dept ON (ticket.dept_id=dept.dept_id) '
.' LEFT JOIN '.TICKET_COLLABORATOR_TABLE.' collab
ON (collab.ticket_id = ticket.ticket_id
AND collab.user_id ='.$thisclient->getId().' )'
.' LEFT JOIN '.$subject_sql.' subject ON ticket.ticket_id = subject.object_id ';
$qwhere = sprintf(' WHERE ( ticket.user_id=%d OR collab.user_id=%d )',
$thisclient->getId(), $thisclient->getId());
if($status){
$qwhere.=' AND ticket.status='.db_input($status);
}
$search=($_REQUEST['a']=='search' && $_REQUEST['q']);
if($search) {
$qstr.='&a='.urlencode($_REQUEST['a']).'&q='.urlencode($_REQUEST['q']);
if(is_numeric($_REQUEST['q'])) {
$qwhere.=" AND ticket.`number` LIKE '$queryterm%'";
} else {//Deep search!
$queryterm=db_real_escape($_REQUEST['q'],false); //escape the term ONLY...no quotes.
$qwhere.=' AND ( '
." subject.value LIKE '%$queryterm%'"
." OR thread.body LIKE '%$queryterm%'"
.' ) ';
$deep_search=true;
//Joins needed for search
$qfrom.=' LEFT JOIN '.TICKET_THREAD_TABLE.' thread ON ('
.'ticket.ticket_id=thread.ticket_id AND thread.thread_type IN ("M","R"))';
}
}
$total=db_count('SELECT count(DISTINCT ticket.ticket_id) '.$qfrom.' '.$qwhere);
$page=($_GET['p'] && is_numeric($_GET['p']))?$_GET['p']:1;
$pageNav=new Pagenate($total, $page, PAGE_LIMIT);
$pageNav->setURL('tickets.php',$qstr.'&sort='.urlencode($_REQUEST['sort']).'&order='.urlencode($_REQUEST['order']));
//more stuff...
$qselect.=' ,count(attach_id) as attachments ';
$qfrom.=' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach ON ticket.ticket_id=attach.ticket_id ';
$qgroup=' GROUP BY ticket.ticket_id';
$query="$qselect $qfrom $qwhere $qgroup ORDER BY $order_by $order LIMIT ".$pageNav->getStart().",".$pageNav->getLimit();
//echo $query;
$res = db_query($query);
$showing=($res && db_num_rows($res))?$pageNav->showing():"";
$showing.=($status)?(' '.ucfirst($status).' Tickets'):' All Tickets';
if($search)
$showing="Search Results: $showing";
$negorder=$order=='DESC'?'ASC':'DESC'; //Negate the sorting
?>
<h1>Tickets</h1>
<br>
<form action="tickets.php" method="get" id="ticketSearchForm">
<input type="hidden" name="a" value="search">
<input type="text" name="q" size="20" value="<?php echo Format::htmlchars($_REQUEST['q']); ?>">
<select name="status">
<option value="">— Any Status —</option>
<option value="open"
<?php echo ($status=='open')?'selected="selected"':'';?>>Open (<?php echo $thisclient->getNumOpenTickets(); ?>)</option>
<?php
if($thisclient->getNumClosedTickets()) {
?>
<option value="closed"
<?php echo ($status=='closed')?'selected="selected"':'';?>>Closed (<?php echo $thisclient->getNumClosedTickets(); ?>)</option>
<?php
} ?>
</select>
<input type="submit" value="Go">
</form>
<a class="refresh" href="<?php echo Format::htmlchars($_SERVER['REQUEST_URI']); ?>">Refresh</a>
<table id="ticketTable" width="800" border="0" cellspacing="0" cellpadding="0">
<caption><?php echo $showing; ?></caption>
<thead>
<tr>
<th width="70" nowrap>
Ticket #
</th>
<th width="100">
Create Date
</th>
<th width="80">
Status
</th>
<th width="300">
Subject
</th>
<th width="150">
Department
</th>
<th width="100">Phone Number</th>
</tr>
</thead>
<tbody>
<?php
if($res && ($num=db_num_rows($res))) {
$defaultDept=Dept::getDefaultDeptName(); //Default public dept.
while ($row = db_fetch_array($res)) {
$dept=$row['ispublic']?$row['dept_name']:$defaultDept;
$subject=Format::htmlchars(Format::truncate($row['subject'],40));
if($row['attachments'])
$subject.=' <span class="Icon file"></span>';
$ticketNumber=$row['number'];
if($row['isanswered'] && !strcasecmp($row['status'],'open')) {
$subject="<b>$subject</b>";
$ticketNumber="<b>$ticketNumber</b>";
}
$phone=Format::phone($row['phone']);
if($row['phone_ext'])
$phone.=' '.$row['phone_ext'];
?>
<tr id="<?php echo $row['ticket_id']; ?>">
<td class="centered">
<a class="Icon <?php echo strtolower($row['source']); ?>Ticket" title="<?php echo $row['email']; ?>"
href="tickets.php?id=<?php echo $row['ticket_id']; ?>"><?php echo $ticketNumber; ?></a>
</td>
<td> <?php echo Format::db_date($row['created']); ?></td>
<td> <?php echo ucfirst($row['status']); ?></td>
<td>
<?php echo $subject; ?>
</td>
<td> <?php echo Format::truncate($dept,30); ?></td>
<td><?php echo $phone; ?></td>
</tr>
<?php
}
} else {
echo '<tr><td colspan="7">Your query did not match any records</td></tr>';
}
?>
</tbody>
</table>
<?php
if($res && $num>0) {
echo '<div> Page:'.$pageNav->getPageLinks().' </div>';
}
?>
$limit='';
$i=0;
$q=mysql_query(...)
while($i<=mysql_num_rows($q))
{
$b=$i+5;
$limit='limit '.$i.', '.$b;
mysql_query(...$limit)
}

Categories