Search multiple tables and display links to different files - php

I have 2 tables first one tblactive ( ac_id, ac_title, ac_text) , second one tblnews (n_id, n_title, n_text)
Also I have 3 files
Active.php (in this file I obtain data from table active and display it through function displayac, ex. <a href=active.php?op=displayac&ac_id=$ac_id>$ac_title</a>)
News.php (in this file I obtain data from table news and display it through function displaynews, ex. <a href=news.php?op=displaynews&n_id=$n_id>$n_title</a>)
The problem
Search.php ( in this file I want to search the (searchword like ac_text and searchword like n_text in the 2 tables active & news at same time and display result of search containing links to their original files ex.
This my query
$result ="(select n_title,n_text, n_id from tblnews where n_text LIKE '%$searchword%')
union
(select ac_title,ac_text, ac_id from tblactive where ac_text LIKE '%$searchword%') limit $page, $perpage";
$query=mysql_query($result);
If the searchword found in ac_text column the link will be <a href=active.php?op=displayac&ac_id=$ac_id>$ac_title</a>
And if searchword found in n_text column the link will be <a href=news.php?op=displaynews&n_id=$n_id>$n_title</a>

Union will not tell you from which table the data came, so manipulate your query as
(select n_title,'n' as wchtab, n_text, n_id from tblnews where n_text LIKE '%e%')
union
(select ac_title,'a' as wchtab, ac_text, ac_id from tblactive where ac_text LIKE '%e%')
where 'wchtab' tells you from which table the data came from, now you can check while fetching the records like
while ($row = mysql_fetch_assoc($result)) {
if($row['wchtab'] == 'n'){
$n_title = $row['N_TITLE'];
$n_id = $row['N_ID'];
echo "<a href=news.php?op=displaynews&n_id=$n_id>$n_title</a>"
}
else if($row['wchtab'] == 'a'){
$ac_title = $row['N_TITLE'];
$a_id = $row['N_ID'];
echo "<a href=active.php?op=displayac&ac_id=$ac_id>$ac_title</a>";
}
NOTE as you are using union, you will get the title of the column as the first table name column fields, so you won't get ac_title separately, but the column n_title will hold data for ac_title and wchtab will tell you from which table the data has came.
}

Related

From two tables from my database i would like to echo a column with click counts

In my database i have created two tables , the one is "categories" and the other "click_count".
The two tables have the following information : categories( cat_id, cat_name , cat_description ) and click_count(id, cat_id, cat_count). I have already written a php code which echo a table with information about categories and i have already written a php code whick calculates the click counts, so i want a php script which i can echo on the same table the information about click_count and specify the "cat_count" which contains the number about "clicks" . The following code is obviously wrong but you can get the logic.
<?php
$sql4 = "SELECT categories.cat_id,categories.cat_name,click_count.cat_id,click_count.cat_count WHERE categories.cat_id=click_count.cat_id";
$result4 = mysqli_query($conn, $sql4);
$row4 = mysqli_fetch_array($result4);
while($row4 = mysqli_fetch_assoc($result4)) {
echo '<td>'.$row4['cat_count']; }?>
The SELECT statement should contain a JOIN:
SELECT categories.cat_id, categories.cat_name, click_count.cat_id, click_count.cat_count
FROM categories
LEFT JOIN click_count
ON categories.cat_id = click_count.cat_id;
...and you can also add a WHERE clause at the end if you need it to select not all, but only the ones that fit a certain condition.

How to randomly selected column from two tables in database PHP

i want to make reading toefl test. i have two tables named 'id_reading' and 'soal_reading'. id_reading table contain id and text. soal_reading contain id, question, option a b c d and answer. so i want to display data from that tables. but i want to display randomly all column in soal_reading tables except id column. i try but it wont random. please help.
<?php
include "conection.php";
$query = mysql_query("
SELECT id_reading.id
, text
, id_reading.text
, soal_reading.pertanyaan
, a
, b
, c
, d
, jawaban
from id_reading
, soal_reading
where id_reading.id = soal_reading.id
");
if ($query) {
while ($row = mysql_fetch_array($query)) {
echo "
<tr>
<td>".$row['id']."</td>
<td>".$row['text']."</td>";
$q = mysql_query("SELECT * from soal_reading order by rand()");
if ($q) {
while ($r = mysql_fetch_array($query)) {
echo "
<td>".$r['pertanyaan']."</td>
<td>".$r['a']."</td>
<td>".$r['b']."</td>
<td>".$r['c']."</td>
<td>".$r['d']."</td>
<td>".$r['jawaban']."</td>
<td>
Edit |
Delete
</td>
</tr>";
}
}
}
}
?>
Your query set in a random way questions but not fields in the question. So if you have 3 questions for example Q1, Q2,Q3 they are ordered in a random way, not the fields.
I suggest you to put your $r['a'] .... $r['d'] into an array and then extract a number from 0 to count($array) to get a possible answer and remove that element from the array. Do that until all possible answers are extracted. (count($array)==0).
In summary, your query provides you random order for your questions and in PHP you give a random order to your fields.
For some code, please comment.
Bye

Working with 2 tables PHP & MySQL

I am working on a webpage that displays list of shops. I have 2 tables, shops and shops_sched.
+-shops-+
| id | title |
+-------------shops_sched-------------+
| id | shops_id | start_date | end_date |
Basically, the program displays the list of shops from the shops table, but if a value from shops.id is found # shops_sched.shops_id the page must output shops.title + 'coming soon'.
I understand this will be easy if I just place the date fields inside the table shops but due to programming restrictions I can't. I'm working on an existing project and I'm trying to minimize changes to existing functions. I can create new PHP functions if necessary though.
In addition, I need to get all the entries from the shops table. The Program needs to return all shops.title but for those shops whose id is found # shops_sched.shops_id, the program will have to return shops.title + "Coming Soon".
must output shops.title + 'coming soon'.
So do it like this:
$shops.title = "Donut-John";
echo $shops.title." coming soon";
To join the shops and shops_sched table
$query = SELECT `title` FROM `shops` JOIN `shops_sched` ON `shops`.`id` = `shops_sched`.`shops_id` WHERE `shops_sched`.`shop_id` = 5;
$result = mysql_query($query);
while($row = mysql_fetch_array($result) {
echo $row['title'] . 'coming soon';
}
For more about join you also can refer the following link
https://dev.mysql.com/doc/refman/5.0/en/join.html
http://www.tutorialspoint.com/mysql/mysql-using-joins.htm
Join the two tables :
SELECT shops.title
FROM shops INNER JOIN shops_sched ON shops.id = shops_sched.shops_id
The query should return only the the shops inside shops_sched
EDIT :
If I understood your question, try this :
SELECT shops.title, shops_sched.id
FROM shops LEFT JOIN shops_sched ON shops.id = shops_sched.shops_id
This will return all the titles, and the shops_sched.shops_id if shops.id = shops_sched.shops_id. In the other case, the hops_sched.shops_id will be null
Then you fetch the rows and if the second row is not null, print title + coming soon
Sample code : (Something like this)
$query = "SELECT `title`, 'shops_id' FROM `shops` LEFT JOIN `shops_sched` ON `shops`.`id` = `shops_sched`.`shops_id` WHERE `shops_sched`.`shop_id`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result) {
if($row['shops_id'] != "")
{
echo $row['title'] . ' coming soon';
}
else
{
echo $row['title'];
}
}

How to show data from two tables php mysql by avoiding repeats?

Here is what i am doing I have two tables one is categories and other is softwares
Category table Looks like this
cid name
1 Anti-malware
And Software Table looks like this
id title catid
1 Avast 1
And catid is equal cid you can say that catid is id of category
here is my current code
require('config/db.php');
$cateq="SELECT * FROM softwares A INNER JOIN categories B ON A.catid=B.cID ";
$cateresult=mysql_query($cateq);
define('MAX_TITLES_PER_CAT',5);
$cat='';
$tcount=0;
while ($row =mysql_fetch_array($cateresult)){
$title=$row['title'];
$softid=$row['id'];
$cateid=$row['cid'];
$check_pic = "admin/images/icons/".$softid."/image01.jpg";
if (file_exists($check_pic)) {
$icon = "<img src=\"$check_pic\" class=\"home_icon\" />";
} else {
$icon = "<i class=\"icon-archive\"></i>";
}
if ($row['name']!=$cat) {
if ($cat!='') echo '</div>';
$cat=$row['name'];
$tcount=MAX_TITLES_PER_CAT;
echo "<div class=\"widget white_box\"><h3 class=\"widget_title\">".$cat." <i class=\"icon-chevron-right\"></i></h3><ul>";
}
if ($tcount--<=0) continue;
echo '<li>'.$icon.' <a href=\'software-profile.php?pid='.$softid.'\'>'.$title.'</a></li>';
}
if ($cat!='') echo '</ul></div>';
and here are the results what i am getting
http://postimg.org/image/d7zn2k6ev/full/
its repeating certain categories in different divs instead of showing in same div all the results.
When you parse your query results, you assume the rows are sorted by category (if ($row['name']!=$cat)), but no order is guaranteed without an ORDER BY clause.
Add ORDER BY name to the end of your query.

Trick Question; Showing 'nr of results' of MySql search

I have a classifieds website, and users can search ads.
The results are displayed in three tabs on top of the page. These three are "All", "Private", and "Company".
Each tab has a nr attached to it, which represents the nr of ads found in that tab.
So for example:
All Private Company
5 3 2
All is just a total of private+company!
I am using MySql as a database.
I am trying to figure out a way to find out these "numbers of ads found" for each tab.
I have one way of doing this, which is like this, but gives me a headache because it is so messy:
$query = "SELECT SQL_CACHE * FROM classified WHERE classified.classified_id=$id";
if ($adtypes=="Private"){
$query_priv_comp = "SELECT SQL_CACHE * FROM classified WHERE priv_comp='Company'";
}
else {
$query_priv_comp = "SELECT SQL_CACHE * FROM classified WHERE priv_comp='Private'";
}
switch ($adtypes){
case "Private":
$query.= " AND classified.priv_comp='Private'";
break;
case "Company":
$query.= " AND classified.priv_comp='Company'";
break;
}
$qry_result = mysql_query($query); // main query
$result_priv_comp = mysql_query($query_priv_comp); // second query
$num_priv_comp = mysql_num_rows($result_priv_comp);
if ($adtypes=="All"){
$num_total = mysql_num_rows($qry_result);
}
else if ($adtypes!="All"){
$num_total=mysql_num_rows($qry_result) + mysql_num_rows($result_priv_comp);
}
if ($adtypes=="Private"){
$num_private = $num_total - $num_priv_comp;
$num_company = $num_priv_comp;
}
else {
$num_company = $num_total - $num_priv_comp;
$num_private = $num_priv_comp;
}
Do you know of any other way which this can be done?
Thanks
BTW: I need the rows too, in order to display information to the user of the ads found!
It depends on what you need exactly. If you just need the counts it's relatively easy:
SELECT count(*) count_all
, sum(if(priv_com = 'Private', 1, 0)) count_private
, sum(if(priv_com = 'Company', 1, 0)) count_company
FROM classified
WHERE classified.classified_id=$id
If on the other hand, you need both counts as well as row data, you should either do separate queries for the data and the counts, or use a trick. Let's say your table has an id column wich is primary key, you could do:
SELECT count(*) count_all
, sum(if(priv_com = 'Private', 1, 0)) count_private
, sum(if(priv_com = 'Company', 1, 0)) count_company
, classified.*
FROM classified
WHERE classified.classified_id=$id
GROUP BY id -- group by on primary key
WITH ROLLUP
The WITH ROLLUP magic will give you an extra row with the counts for the entire query. The only snag is that you will receive this row as last row of the entire result, so if you want to report the counts before the data, you're going to have to cache the row data in an php array or so and process that later to build up the page.
After your switch variable
$query_priv_comp would be equal to:
SELECT SQL_CACHE * FROM classified WHERE priv_comp='Company'
AND classified.priv_comp='Private'
or
SELECT SQL_CACHE * FROM classified WHERE priv_comp='Private'
AND classified.priv_comp='Company'
Question: What the difference???
You can select all counts with one query:
SELECT priv_comp, COUNT(*) AS record_count FROM classified GROUP BY priv_comp
Then you can query all the records needed for the current tab.
These 2 should be separated clearly.

Categories