I have a database with tables: questions, answers, responses, and respondents. Questions have answers, which have responses, which are owned by a respondent (Respondents can have many responses).
I'm trying to make correlations by zeroing in on all the responses to questions related through respondents who chose a certain answer to a question. So, what else did people who answered that they were 18 - 20, answer.
The trick is I want to do this across multiple respondents and multiple answers. For example, what else did people who are 18 - 20, and like the color green like:
I currently have this working in an "or" fashion where each additional answer_id I add pulls in respondents who chose either one of the two answers:
$cFilters = array() of answer_ids;
$respondents = Auth::user()->account->respondents()
->whereHas('responses', function($q) use($cFilters) {
$q->whereIn('responses.answer_id', $cFilters);
})->get();
I'd ultimately like to have them working in an "and" fashion where adding in another answer_id shows only respondents who have chosen both answers identified by the answer_ids, can't quite figure that one out. Here is what I've tried so far that doesn't raise errors, but also doesn't seem to work correctly:
$cFilters = array() of answer_ids;
$respondents = Auth::user()->account->respondents()
->whereHas('responses', function($q) use($cFilters) {
foreach($cFilters as $answer_id) {
$q->where('responses.answer_id', $answer_id);
}
})->get();
I've had to resort to raw SQL, but it works:
$query = 'SELECT * FROM respondents
WHERE id IN ( ';
$i = 1;
foreach($cFilters as $answer_id) {
$query = $query . 'SELECT respondent_id FROM responses WHERE answer_id=' . $answer_id;
if($i < count($cFilters)) {
$query = $query . ') AND id IN (';
}
$i++;
}
$query = $query . ')';
$respondents = DB::select( DB::raw($query) );
Related
http://projects.ourplanet.tk/junetxtdb/
http://code.google.com/p/junetxtdb/ in cms (LekkiCMS)
Now my question is how change this code :
$query = $db->select('wizyty');
foreach($query as $record) {
$idz = $record['pro'];
$query2 = $db->select('wizyty_zabiegi',array('id'=>$idz));
foreach($query2 as $record2) {
$record2['name'];
}
}
Every table has lots of values
table wizyty has organized this way
ID"PID"pro"date"time"value1"value2
1"1"2"2020-12-30"12:00"1"2
2"1"1"2020-05-30"12:00"1"2
and table wizyty_zabiegi this way
ID"name"TIMES"HOW
1"Masaż stóp"25"100
2"Masaż nóg"25"100
i use even for (valuepro = valueid)
but this show only good in first result
third, five , six etc result dont show correctly
thank you ;)
i know in mysql i have join but in this project i dont have it ;(
i wrote this question and i found solution hahah
$is= $record['zabieg'] -1;
$query2 = $db->select('wizyty_zabiegi');
$result .= $query2[$is]['nazwa'];
thanks for everything even for reading this messages
I'm developping a quiz app, and for this I use 2 different tables:
One is "question", where there are questions and answers
the other one is "answer_quizz_user" with a unique id depending on the candidate, where I have exactly the same column than "question", with the answer in a random order.
My goal is to show the same question with the same answer in a random order, and to save it in the db like this.
This is what my table looks like
I would like to select the column by pair
(answer"X"_right_answer is a boolen which mean right or wrong answer):
(answer1,answer1_right_answer),
(answer2,answer2_right_answer),
(answer3,answer3_right_answer),
(answer4,answer4_right_answer),
(answer5,answer5_right_answer),
(answer6,answer6_right_answer)
to shuffle it, and insert it in "answer_quizz_user".
I tried different things like :
for ($n=1; $n <=6 ; $n++) {
INSERT INTO answer_quizz_user SELECT
answer".$n." FROM question
WHERE question_question = "q1"
ORDER BY RAND()}
but none worked. I have absolutely no idea how to manage this select->insert. I know that I should have done another table for answers, but now it's too late, the app is almost finish, I just need this random part...
Thanks for your help and sorry for my bad english..
Something like this should work, although your database structure is quite bad. Next time use a separate table for your answers.
It isn't a copy-and-paste solution (I would needed to know more about your code to do that), but I hope it will be enough to set you on the right path.
The basic idea is, you get your pairs from the db, put them in an array, use php's built-in shuffle() function, then insert your rows.
// Gets the required row from database
$query = "select * from question where question_question = 'q1';";
$result = $db->query($query);
if($result->num_rows > 0) {
$question = $result->fetch_assoc();
// Create an array with the pairs
$pairs = array();
for($n = 1; $n <= 6; $n++) {
$pairs[] = array(
'Answer' => $question['answer' . $n],
'IsRight' => $question['answer' . $n . '_right_answer'],
);
}
// Arrange pairs in random order
shuffle($pairs);
// Insert the new rows
foreach($pairs as $pair) {
$query = "insert into answer_quizz_user values ('" . $pair['Answer'] . "', '" . $pair['IsRight'] . "');";
$db->query($query);
}
}
I hope, I could be of any help.
I wasn't able to find anything that really helped me out with this.
I have a database with the following structure:
and a simple query to search and display some results from the DB:
if ($_REQUEST['search']) {
$q = $_REQUEST['search'];
$q_comma = explode(", ", $q);
$where_in_set = '';
$count = count($q_comma);
foreach ($q_comma as $q) {
$counter++;
if ($counter == $count) {
$where_in_set .= "FIND_IN_SET('$q','keywords')";
} else {
$where_in_set .= "FIND_IN_SET('$q','keywords') AND ";
}
}
$sql_res = "select link, description, keyword from myDB where $where_in_set or description like '%$q%'";
This code works, but not really as I wanted.
In the keyword column, I have different comma separated keywords, and i'd like to be able to search for them even if the order is different.
Here's an example: Let's say I have into my keyword column
Google, Facebook, twitter
With my current code if I type Google, I can see the result, but if i type twitter, I don't see it.
Is there anything I can do to make it work without taking into account the order of the keywords, but having a pretty fast search as well?
Any help will be really appreciated.
PS. I'd like to keep only one DB if possible, cause I read about creating a new table with only ID and keywords, and on my search join the tables on the ID's, but I would prefer a better solution if possible.
Thanks
EDIT
Some updates:
as pointed out by #Frayne Konok, i have the query in lowcase and all the value in the db in lowcase as well, so case cannot be the problem
As suggested by #mkaatman, i wrapped keyword column around backticks (`)
I changed my query so that now it looks like the one suggested by #user2272989 in the answer
So my query now looks like this:
select link, description, keyword from myDB
where (FIND_IN_SET('google',`keyword`) or
FIND_IN_SET('facebook', `keyword`) or
FIND_IN_SET('twitter', `keyword`))
OR description like 'google, facebook, twitter'
And it is returning values even if the order is different, but it is not showing only the one i want.
For example, if i write twitter, google, facebook, i have as a return something like 28 rows, where only 2 have all of the three words as a keyword, while the other my have only one or two
EDIT 2 - Updates
I just want to "reopen" this question since I didn't manage to solve this. If I change all the keywords into an object, will then be better to use them as keywords? or what is the absolute best and more reliable way to search a database for keywords? Having different DBs and use an INNER JOIN?
At this point I'm willing to change the structure and the code if it helps.
Thanks
This may help you
$sql_res = "select link, description, keyword from myDB
where (FIND_IN_SET('yahoo',keyword) or
FIND_IN_SET('twitter',keyword)) or
description like '%$q%'";
You are using AND condition in FIND_IN_SET
I ended up using this structure and query here:
$q = $_REQUEST['search'];
$q_comma = array_filter(explode(' ', str_replace(',', ' ', $q)));
$count = count($q_comma);
$dbQuery = "select id, link, description, tags from db where ";
$searchTerms = '';
foreach ($q_comma as $q) {
$counter++;
if ($counter == 1) {
$searchTerms .= "tags LIKE '%$q%'";
} else {
$searchTerms .= "and tags LIKE '%$q%'";
}
}
$sql_res = $dbQuery . $searchTerms;
$result = mysqli_query($db, $sql_res) or die(mysqli_error($db));
$return_arr = array();
$data = array();
if (mysqli_num_rows($result) == 0) {
echo "Nothing";
} else {
while ($row = mysqli_fetch_object($result)) {
$data[] = $row;
}
mysqli_free_result($result);
echo json_encode($data);
}
I have a follow-up to a previous thread/question that I hope can be solved by relatively small updates to this existing code. In the other thread/question, I pretty much solved a need for a nested unordered list. I needed the nested unordered list to be broken up into columns based on the number of topics.
For example, if a database query resulted in 6 topics and a user specified 2 columns for the layout, each column would have 3 topics (and the related news items below it).
For example, if a database query resulted in 24 topics and a user specified 4 columns for the layout, each column would have 6 topics (and the related news items below it).
The previous question is called PHP - Simple Nested Unordered List (UL) Array.
The provided solution works pretty well, but it doesn't always divide
correctly. For example, when $columns = 4, it only divides the
columns into 3 groups. The code is below.
Another issue that I'd like to solve was brought to my attention by
the gentleman who answered the question. Rather than putting
everything into memory, and then iterating a second time to print it
out, I would like to run two queries: one to find the number of
unique TopicNames and one to find the number of total items in the
list.
One last thing I'd like to solve is to have a duplicate set of
code with an update that breaks the nested unordered list into columns
based on the number of news items (rather than categories). So, this
would probably involve just swapping a few variables for this second
set of code.
So, I was hoping to solve three issues:
1.) Fix the division problem when relying on the number of categories (unordered list broken up into columns based on number of topics)
2.) Reshape the PHP code to run two queries: one to find the number of unique TopicNames and one to find the number of total items in the list
3.) Create a duplicate set of PHP code that works to rely on the number of news items rather than the categories (unordered list broken up into columns based on number of news items)
Could anyone provide an update or point me in the right direction? Much appreciated!
$columns = // user specified;
$result = mysql_query("SELECT * FROM News");
$num_articles = 0;
// $dataset will contain array( 'Topic1' => array('News 1', 'News2'), ... )
$dataset = array();
while($row = mysql_fetch_array($result)) {
if (!$row['TopicID']) {
$row['TopicName'] = 'Sort Me';
}
$dataset[$row['TopicName']][] = $row['NewsID'];
$num_articles++;
}
$num_topics = count($dataset);
// naive topics to column allocation
$topics_per_column = ceil($num_topics / $columns);
$i = 0; // keeps track of number of topics printed
$c = 1; // keeps track of columns printed
foreach($dataset as $topic => $items){
if($i % $topics_per_columnn == 0){
if($i > 0){
echo '</ul></div>';
}
echo '<div class="Columns' . $columns . 'Group' . $c . '"><ul>';
$c++;
}
echo '<li>' . $topic . '</li>';
// this lists the articles under this topic
echo '<ul>';
foreach($items as $article){
echo '<li>' . $article . '</li>';
}
echo '</ul>';
$i++;
}
if($i > 0){
// saw at least one topic, need to close the list.
echo '</ul></div>';
}
UPDATE 12/19/2011: Separating Data Handling from Output Logic (for the "The X topics per column variant"):
Hi Hakre: I've sketched out the structure of my output, but am struggling with weaving the two new functions with the old data handling. Should the code below work?
/* Data Handling */
$columns = // user specified;
$result = mysql_query("SELECT * FROM News LEFT JOIN Topics on Topics.TopicID = New.FK_TopicID WHERE News.FK_UserID = $_SESSION[user_id] ORDER BY TopicSort, TopicName ASC, TopicSort, NewsTitle");
$num_articles = 0;
// $dataset will contain array( 'Topic1' => array('News 1', 'News2'), ... )
$dataset = array();
while($row = mysql_fetch_array($result)) {
if (!$row['TopicID']) {
$row['TopicName'] = 'Sort Me';
}
$dataset[$row['TopicName']][] = $row['NewsID'];
$num_articles++;
}
/* Output Logic */
function render_list($title, array $entries)
{
echo '<ul><li>', $title, '<ul>';
foreach($entries as $entry)
{
echo '<li>', $entry['NewsID'], '</li>';
}
echo '</ul></li></ul>;
}
function render_column(array $topics)
{
echo '<div class="column">';
foreach($topics as $topic)
{
render_list($topic['title'], $topic['entries']);
}
echo '</div>';
}
You have not shown in your both questions what the database table is, so I can not specifically answer it, but will outline my suggestion.
You can make use of aggregation functions in mysql to obtain your news entries ordered and grouped by topics incl. their count. You can do two queries to obtain counts first, that depends a bit how you'd like to deal with your data.
In any case, using the mysql_... functions, all data you selected from the database will be in memory (even twice due to internals). So having another array as in your previous question should not hurt much thanks to copy on write optimization in PHP. Only a small overhead effectively.
Next to that before you take care of the actual output, you should get your data in order so that you don't need to mix data handling and output logic. Mixing does make things more complicated hence harder to solve. For example if you put your output into simple functions, this gets more easy:
function render_list($title, array $entries)
{
echo '<ul><li>', $title, '<ul>';
foreach($entries as $entry)
{
echo '<li>', $entry['NewsID'], '</li>';
}
echo '</ul></li></ul>;
}
function render_column(array $topics)
{
echo '<div class="column">';
foreach($topics as $topic)
{
render_list($topic['title'], $topic['entries']);
}
echo '</div>';
}
This already solves your output problem, so we don't need to care about it any longer. We just need to care about what to feed into these functions as parameters.
The X topics per column variant:
With this variant the data should be an array with one topic per value, like you did with the previous question. I would say it's already solved. Don't know which concrete problem you have with the number of columns, the calculation looks good, so I skip that until you provide concrete information about it. "Does not work" does not qualify.
The X news items per column variant:
This is more interesting. An easy move here is to continue the previous topic with the next column by adding the topic title again. Something like:
Topic A Topic A Topic B
- A-1 - A-5 - B-4
- A-2 Topic B - B-5
- A-3 - B-1 - B-6
- A-4 - B-2
- B-3
To achieve this you need to process your data a bit differently, namely by item (news) count.
Let's say you managed to retrieve the data grouped (and therefore sorted) from your database:
SELECT TopicName, NewsID FROM news GROUP BY 1;
You can then just iterate over all returned rows and create your columns, finally output them (already solved):
$itemsPerColumn = 4;
// get columns
$topics = array();
$items = 0;
$lastTopic = NULL;
foreach ($rows as $row)
{
if ($lastTopic != $row['TopicName'])
{
$topic = array('title' => $row['TopicName']);
$topics[] = &$topic;
}
$topic['entries'][] = $row;
$items++;
if ($items === $itemsPerColumn)
{
$columns[] = $topics;
$topics = array();
$lastTopic = NULL;
}
}
// output
foreach($columns as $column)
{
render_column($column);
}
So this is actually comparable to the previous answer, but this time you don't need to re-arrange the array to obtain the news ordered by their topic because the database query does this already (you could do that for the previous answer as well).
Then again it's the same: Iteration over the returned result-set and bringing the data into a structure that you can output. Input, Processing, Output. It's always the same.
Hope this is helpful.
At this time I have this very complex query that I loop through and I get something like this:
List of Challenges:
TEAM A
- Challenge 1
TEAM A
- Challenge 4
TEAM A
- Challege 6
And I want to change to something like:
TEAM A
- Challenge 1
- Challenge 4
- Challenge 6
My question is, since the query is a very complex one, maybe I could do this inside the loop but, if that's the case, how can we achieve something like that?
Can I ask an example case so that I can use, in order to solve this issue?
Thanks a lot,
MEM
UPDATE:
The query is something like this:
Translated:
public function listachallengesPendentes()
{
$select = $this->getAdapter()->select();
$select->from(array("e"=>"teams"),array('name'));
$select->join(array("de"=>"challengeperteam"),"e.cod_team = de.cod_teamFk",array());
$select->join(array("d"=>"challenges"),"d.cod_challenge = de.cod_challengeFk",array('title'));
$select->columns(array("e.cod_team"
,"name_team"=>"e.name"
,"d.cod_challenge"
,"name_challenge"=>"d.title"
,"d.details"
,"d.score"
,"category"=>"d.cod_categoryFk"
,"de.proof"
,"de.date_concluded"
,"de.cod_challenge_team"
));
$select->where("de.status = 0");
$select->order(array('e.cod_team DESC', 'de.cod_challenge_team DESC'));
return $this->getAdapter()->fetchAll($select);
}
So I need to add a distinct some part :s :D ?
The foreach actually is pretty basic:
foreach ($challenges as $d){
//display the name:
echo $d['name_team'];
...
}
UPDATE 2
The clean query (not tested):
SELECT e.name
,d.cod_team
,d.cod_challenge
,d.title
,d.details
,d.score
,de.proof
,de.date_concluded
,de.cod_challenge_team
FROM teams e
INNER JOIN challengeperteam de ON de.cod_teamFk = e.cod_team
INNER JOIN challenges d ON d.cod_challenge = de.cod_challengeFk
WHERE de.status = 0
ORDER BY e.cod_team DESC, de.cod_challenge_team DESC;
Something along the lines of:
$current_team = null;
foreach($challenges as $challenge){
if($current_team != $challenge->team){
$current_team = $challenge->team;
echo $current_team, "\n";
}
echo $challenge->challenge_name, "\n";
}
At a very basic level, ie in the loop, you can just detect if the TEAM A variable is equal to the current (previous) value, and if so, don't print it a second time. This relies on the result set being sorted on the TEAM A column.
However, you can also do this in the SQL query, so if you can provide the current SQL Query, I can explain how you'd update it.
you could store the array results in a multi-dimensional array like so:
$query_Challenges = "SELECT `Team`,`Challenges` FROM YourTable";
$Challenges = mysql_query($query_Challenges, $dbconnection) or die(mysql_error());
$row_Challenges = mysql_fetch_assoc($Challenges);
$challengeResults = array();
do{
if(!array_key_exists($row_Challenges['cod_team'])){
$challengeResults[$row_Challenges['cod_team']] = array();
}
$challengeResults[$row_Challenges['cod_team']][] = $row_Challenges['cod_challenge_team'];
}while($row_Challenges = mysql_fetch_assoc($Challenges));
EDIT
looking at your query statement, the data should be already sorted properly by your ORDER clause, so if you just need not repeatedly print the team as shown in codeblock 2, then something like:
$team = '';
do {
if($team != $row_Challenges['cod_team']){
echo "TEAM $row_Challenges['cod_team']<br/>";
$team = $row_Challenges['cod_team'];
}
echo " - $row_Challenges['cod_challenge_team']<br />";
}while($row_Challenges = mysql_fetch_assoc($Challenges));
you could easily substitute a foreach for the do loop, as long as there is a variable used as the "current team" and an if statement used to say "dont print the next team name unless its different than the current team name"