Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何组合具有两个用户id并在视图中显示的表_Php_Html_Laravel_Sqlite - Fatal编程技术网

Php 如何组合具有两个用户id并在视图中显示的表

Php 如何组合具有两个用户id并在视图中显示的表,php,html,laravel,sqlite,Php,Html,Laravel,Sqlite,我在拉威尔有一个观点问题 比方说 User id|user_name| 1 |Amy| 2 |Bob| 3 |Candy| 4 |Dina| 5 |Edwin| Product id|product_name| 1 |iPhone 10| 2 |Galaxy 10| 3 |Huawei P10| 4 |Vivo R10| 5 |Xiaomi 10| Q&A id| Question | Answer |ask_user_id|ans_user_id|Product_id|

我在拉威尔有一个观点问题

比方说

User
id|user_name|
1 |Amy|
2 |Bob|
3 |Candy|
4 |Dina|
5 |Edwin|

Product
id|product_name|
1 |iPhone 10|
2 |Galaxy 10|
3 |Huawei P10|
4 |Vivo R10|
5 |Xiaomi 10|

Q&A
id| Question |    Answer   |ask_user_id|ans_user_id|Product_id|
1 |Question 1|Answer For Q1|     5     |     1     |     1    |
2 |Question 2|Answer For Q2|     4     |     2     |     3    |
3 |Question 3|Answer For Q3|     3     |     4     |     3    |
4 |Question 4|Answer For Q4|     2     |     4     |     4    |
5 |Question 5|Answer For Q5|     1     |     5     |     5    |
我想让用户提问,让用户回答问题

因此,控制是:

public function getProductDetail($id) {            
/*
Get Questions and Answers Q&A section
*/
    //Get questions here
    $sql = "SELECT USER.user_name as askUser, QA.question as theQuestion, QA.date_of_post FROM USER, QA WHERE id IN (SELECT ask_user_id FROM QA WHERE product_id = ?) AND USER.id = QA.ask_user_id";
    $questions = DB::select($sql, array($id));

   //Get answers here
   $sql = "SELECT USER.user_name as ansUser, QA.answer FROM USER, QA WHERE id IN (SELECT ans_user_id FROM QA WHERE product_id = ?)AND USER.id = QA.ans_user_id";
   $answers = DB::select($sql, array($id));

   return view('includes.products.productDetail', 
    ['questions' => $questions, 'answers' => $answers]);
}
在视图中,我编写了如下代码:

<div class="QA">
 <h2>Questions and Answers</h2>
  <div class="question">
   <ul class="list-unstyled">
    @foreach($questions as $question)
       <div class="shadow p-3 mb-5 bg-white rounded">
         <li>{{ $question -> theQuestion }}</li>
         <li>{{ $question -> askUser }} asked on {{ $question -> date_of_post}}</li>
         </div>
    @endforeach

    @foreach($answers as $answer)
         <li>{{ $answer -> answer }}</li>
         <li>{{ $answer -> ansUser}}</li>
    @endforeach
  </ul>
   </div>
</div>

我不确定问题是来自我的视图还是来自SQL。谢谢

实现这一点的一种方法是循环每个问题的所有答案,然后检查答案是否与问题匹配。(虽然不是最好的方法,但仍然有效)

为此,在答案查询中获取问题id

$sql = "SELECT USER.user_name as ansUser, QA.answer, QA.question, FROM USER, QA WHERE id IN (SELECT ans_user_id FROM QA WHERE product_id = ?)AND USER.id = QA.ans_user_id";
然后更改foreach循环

@foreach($questions as $question)
    <div class="shadow p-3 mb-5 bg-white rounded">
         <li>{{ $question -> theQuestion }}</li>
         <li>{{ $question -> askUser }} asked on {{ $question -> date_of_post}}</li>

         @foreach($answers as $answer)
             @if($answer->question == $question->theQuestion)
                <li>{{ $answer -> answer }}</li>
                <li>{{ $answer -> ansUser}}</li>
            @endif
         @endforeach
    </div>
@endforeach
@foreach($questions as$question)
  • {{$question->theQuestion}
  • {{$question->askUser}}在{{{$question->date\u of_post}上被问到{{{$question->askUser}}
  • @foreach($answers作为$answer) @如果($answer->question==$question->theQuestion)
  • {{$answer->answer}
  • {{$answer->ansUser}
  • @恩迪夫 @endforeach @endforeach
    @foreach($questions as $question)
        <div class="shadow p-3 mb-5 bg-white rounded">
             <li>{{ $question -> theQuestion }}</li>
             <li>{{ $question -> askUser }} asked on {{ $question -> date_of_post}}</li>
    
             @foreach($answers as $answer)
                 @if($answer->question == $question->theQuestion)
                    <li>{{ $answer -> answer }}</li>
                    <li>{{ $answer -> ansUser}}</li>
                @endif
             @endforeach
        </div>
    @endforeach