Okay essentially what I'm trying to achieve is to combine the first query with the second one the trouble I'm having is that I'm unable to array out the results from the first query to then use in the second one. Here is my first query what this achieves is that it selects all the room subjects from my users which typically look like this, welcome to my #room hope you #enjoy once is selects one it will then strip the subject of the words and will only keep the hash-tags for example it will keep #room and #enjoy once its goes through and selects all the hash-tags from all of the room subjects it will then order them from how frequent they are used among subjects of all users.
$sql = mysql_query("SELECT subject FROM `users`");
$row = mysql_fetch_array($sql);
$freqDist = [];
foreach($results as $row)
{
$words = explode(" ", $row);
foreach($words as $w)
{
if (array_key_exists($w, $freqDist))
$freqDist[$w]++;
else
$freqDist[$w] = 1;
}
}
arsort($freqDist);
foreach($freqDist as $word => $count)
{
if (strpos($word, '#') !== FALSE)
echo "$word: $count\n";
else
echo "$word: does not contain hashtag, DROPPED\n";
}
Now what I'm trying to achieve here is that I'm trying to array out the results from the above query so then I can perform an action on each hash-tag which is grabbed from above. Below I have included the code and essentially the problem I face lies solely on this line $all = array("hashtag1", "hashtag2", "hashtag3"); as what I'm trying to achieve is instead of typing in directly the hash-tags I want to perform the action on I would want them to be grabbed via using the above query.
$all = array("hashtag1", "hashtag2", "hashtag3");
$trending = $all;
foreach($trending as $tags)
{
$sql = mysql_query("SELECT * FROM `users` WHERE `subject` LIKE '%$tags%'");
$sums = mysql_query("SELECT SUM(viewers) AS viewers FROM `users` WHERE `subject` LIKE '%$tags%'");
$num_rows = mysql_num_rows($sql);
$row = mysql_fetch_assoc($sql);
$sum = mysql_fetch_assoc($sums);
}
So again just to outline my problem I'm trying to grab the results from the first query and use them as an array with the second query. I apologise for the length of this question I just assumed it would help you better understand my question.
As per my understanding, you want to store the hashtag value in an array. if so then just update your foreach()
$hashWord = array();
foreach($freqDist as $word => $count)
{
if (strpos($word, '#') !== FALSE)
echo "$word: $count\n";
$hashWord[] = $word;
else
echo "$word: does not contain hashtag, DROPPED\n";
}
Related
code:
<?php
include('config.php');
$return_arr = array();
$term = $_GET['term'];
$term = str_replace('.','',$term);
$sql = "SELECT * FROM submission where keyword like '".$term."%' or keyword
like '%".$term."%' ORDER BY CASE WHEN keyword LIKE '".$term."%' THEN 1
ELSE 2 END";
$r = mysqli_query($link,$sql);
while($row = mysqli_fetch_assoc($r))
{
$key = explode(",", $row['keyword']);
foreach ($key as $keyword)
{
$return_arr[] = $keyword;
}
}
echo json_encode($return_arr);
?>
I have created autocomplete suggestion box and its working properly but I have a column name keyword where my data is look line apolo,alto,bmw,camaro,ducati like this where I am using expload function for listing one by one. If I search with (a) alphabet it showing a result but when I want to search with (b) alphabet its show nothing. So, How can I remove this problem ?Please help me.
Thank You
Use the IN filter to get better results for multiple keywords
SELECT keyword FROM submission WHERE keyword IN ('Apolo', 'BMW', ...);
Im building a search engine from scratch.
It scans many different tables in multiple databases.
It searches through only the specified columns.
The search words are explode()'ed case of multiple words search
It iterates with a foreach through the tables.
I'm having trouble structuring the code base with LIMIT. I want it to show only DISTINCT.
For example if I search Joe Schmoe my code will search for first = joe and then for last = schmoe, and it will display a result for each search. But I want it to display only one result for the two searches.
<?php
echo "<h1>Search Results</h1>";
echo "<table style='width: 100%;'>";
$a = $_GET["q"];
$b = explode(" ", $a);
include 'db_connect3.php';
mysql_select_db("db440035579", $con);
foreach($b as $c){
$sql="SELECT * FROM users WHERE first LIKE '%$c%' || last LIKE '%$c%'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<a href='applications.php?act=search&uid=$row[7]'>$row[5], $row[4]</a>";
}
}
mysql_close($con);
?>
First of all http://bobby-tables.com/
Secondly, plugging in PDO takes only a few lines of code.
Thirdly, FULL TEXT SEARCH is much better.
And finally:
Use a array microcaching. I assume $row[0] is the user.id. That way you only get one item per ID
$cache = array();
foreach($b as $c){
$sql="SELECT * FROM users WHERE first LIKE '%$c%' || last LIKE '%$c%'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$cache[$row[0]] = $row;
}
}
foreach($cache as $row){
echo "<a href='applications.php?act=search&uid=$row[7]'>$row[5], $row[4]</a>";
}
how can I count single words appearing in a field of a resultset?
example
id| myfield
1 | pear apple peach watermelon
2 | lime grapes pear watermelon
I want to get 6 because there are 6 unique words
I don't need a fast query, it is just a statistic calculation that will be executed rarely
thanks!
function uniqueWords ($result, $field) {
$words = array();
while ($row = mysql_fetch_assoc($result)) { /* or something else if you use PDO/mysqli/some ORM */
$tmpWords = preg_split('/[\s,]+/', $row[$field]);
foreach ($tmpWords as $tmpWord) {
if (!in_array($tmpWord, $words))
$words[] = $tmpWord;
}
}
return count($words);
}
You can split the results on a space and then add them to an array, such as
foreach($results as $result)
{
$words = explode(" ", $result['myfield']);
$array = array();
foreach($words as $word)
$array[$word] = true;
}
echo count($array);
Probably a better way, but this is quick and dirty
I cannot think of a pure SQL solution - MySQL dosen't really like to split a single row nto many.
A PHP version is easy though:
$sql="CREATE TEMPORARY TABLE words (word VARCHAR(100) PRIMARY KEY)";
//run this according to your DB access framework
$sql="SELECT myfield FROM mytable";
//run this according to your DB access framework
while (true) {
//fetch a row into $row according to your DB access framework
if (!$row) break;
$words=preg_split('/[\s,]+/',$row['myfield']);
foreach ($words as $word) {
//Escape $word according to your DB access framework or use a prepared query
$sql="INSERT IGNORE INTO words VALUES('$word')";
//run this according to your DB access framework
}
}
$sql="SELECT count(*) AS wordcount FROM words";
//run this according to your DB access framework, the result is what you want
$sql="DROP TABLE words";
//run this according to your DB access framework
I have searched for similar questions but cannot find the right answer to my specific one. I have a column (data) in my table (table) which contains comma separated values which are id's e.g.
Row 1= 4,5,45
Row 2= 5,8,9
Row 3= 5
I use an in_array function to retrieve the number of occurances for the $data value within the while loop. So I use an sql function to retrieve the number of times a certain value such as '5' occurs in all rows within the while loop.
The issue is that I can only retrieve the $data value if it is by itself (i.e. no commas just the integer by itself) so based on my example in the list, I can only retrieve 5 once (row 3). I would like to retrieve the value '5' three times as it appears in all the rows. Here is my code below and any help would be appreciated. The $selectiontext variable is what the user enters from the form.
$sql_frnd_arry_mem1 = mysql_query("SELECT data, id FROM table WHERE data='$selectiontext'");
while($row=mysql_fetch_array($sql_frnd_arry_mem1)) {
$datacheck = $row["data"];
$id = $row["id"];
}
$frndArryMem1 = explode(",", $frnd_arry_mem1);
if (($frnd_arry_mem1 !=="") && (!in_array($id, $frndArryMem1))) {echo $id;}
Thank you.
you keep overwriting the variables $datacheck and $id, leaving you with only the last version of them. move that curly brace down two lines, like this:
$sql_frnd_arry_mem1 = mysql_query("SELECT data, id FROM table WHERE data='$selectiontext'");
while($row=mysql_fetch_array($sql_frnd_arry_mem1)) {
$datacheck = $row["data"];
$id = $row["id"];
$frndArryMem1 = explode(",", $frnd_arry_mem1);
if (($frnd_arry_mem1 !=="") && (!in_array($id, $frndArryMem1))) {echo $id;}
}
I am not totally sure of your requirements, is not very clear what you intend to do as part of the code is missing.
I assume you want to check each row of a comma separated of a CSV file ($frnd_arry_mem1 being the row) against your data in column data, if any of those comma separated data (not) occurs.
Assuming you are already looping through your CSV, this would be the code (non tested):
NOTE: might be inefficient retrieving data within a loop, if you can do otherwise - but I cant tell as I dont kow the full specs of your script.
If I got the specs wrong please clarify, I will try to help further.
$sql = "SELECT data, id
FROM table
WHERE data='$selectiontext'";
$sql_frnd_arry_mem1 = mysql_query($sql);
$results = array();
// get values to check
while ($row = mysql_fetch_array($sql_frnd_arry_mem1))
{
$results[] = array(
'datacheck' => $row["data"],
'id' => $row["id"],
);
}
foreach ($results as $result)
{
$frndArryMem1 = explode(",", $frnd_arry_mem1);
$data = explode(',', $result['datacheck']);
foreach ($data as $d)
{
if (($frnd_arry_mem1 !=="") && (!in_array($d, $frndArryMem1)))
{
echo 'DEBUG: Record #' . $id . ' not found, value:' . $d . '</br>';
}
}
}
How can I get the frequency of an item in a field contained in the following array?
//
...
...
//database stuff
while($row=mysql_fetch_array($result))
{
//Retrieve tags for this article
$tags=$row["art_tags"];
$tagarray=explode(",",$tags);
foreach($tagarray as $key => $value)
{
//need solution here
//how to Search entire art_tags field in the database and get frequency of all tags
echo $value;
Eg.
// Rice x 23 times
// Beans x 12 times
}
To count up all the tags in your database use array_count_values():
$all_tags = array();
while ($row = mysql_fetch_assoc($result)) {
$all_tags = array_merge($all_tags, explode(',', $row['art_tags']));
}
$all_tags = array_count_values($all_tags);
echo $all_tags['rice'];
Or, for one just tag, let the database do the work for you with COUNT(*):
$tag = 'rice';
$sql = "SELECT COUNT(*) FROM articles
WHERE art_tags LIKE '$tag'
|| art_tags LIKE '$tag,%'
|| art_tags LIKE '%,$tag'
|| art_tags LIKE '%,$tag,%'";
$result = mysql_query($sql);
list($number) = mysql_fetch_row($result);
One way that you could do this is to use the sql count function. Then you wouldn't need to do this programatically. Though, if you have no control over the query and have to do this in code, you would either have to iterate through the array and check to see if the tag that you are looking for is contained inside each record. Unless the query is already finding only those instances of the tags that you need then just get the size of the array.
you can try this code:
foreach($tagarray as $key => $value
{
$tag_count[$value] += 1;
}
and then:
foreach ($tag_count as $tag => $count)
{
echo $tag.'x'.$count.'times';
}