Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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 我想让不同故事的收藏夹按钮正常工作_Php_Database_Laravel_If Statement - Fatal编程技术网

Php 我想让不同故事的收藏夹按钮正常工作

Php 我想让不同故事的收藏夹按钮正常工作,php,database,laravel,if-statement,Php,Database,Laravel,If Statement,我在PHP Laravel项目中面临一个问题。 我有一个最喜欢的故事按钮。不同的用户将有一个选择,使最喜欢的故事,他们喜欢。“故事”和“最喜欢的故事”有单独的表格。我已经根据用户id从stories表中获取了所有故事,从fav_story表中获取了fav_故事 这是我的控制器代码 public function fanfiction(){ $user_id = session('userid'); $data['stories'] = DB::Table('stories')-&

我在PHP Laravel项目中面临一个问题。 我有一个最喜欢的故事按钮。不同的用户将有一个选择,使最喜欢的故事,他们喜欢。“故事”和“最喜欢的故事”有单独的表格。我已经根据用户id从stories表中获取了所有故事,从fav_story表中获取了fav_故事

这是我的控制器代码

public function fanfiction(){
    $user_id = session('userid');
    $data['stories'] = DB::Table('stories')->orderBy('story_id', 'des')->get();
    $data['fav_story'] = DB::Table('favorite_story')->where('user_id', $user_id)->get();
    return view('fanfiction', $data);
}
这是我的视图代码

@foreach($stories as $row)
    <?php $story_id = $row->story_id; ?>
    <article class="post excerpt">
        <a href="{{ url('read_fanfic/'.$row->story_id) }}" id="featured-thumbnail">
            <div class="featured-thumbnail">
                @if($row->img == '')
                <img class="img-responsive" width="30%" src="{{ url('public/uploads/fanfic/no_img.png') }}" alt="Story Image" />
                @else
                <img class="img-responsive"  width="30%" src="{{ url('public/uploads/fanfic/'.$row->img) }}" alt="Story Image" />
                @endif
            </div>                      
        </a>
        <div class="post-content" style="text-align:justify;">
            {{ $row->story_desc }};
            <h3>{{ $row->story_title }}</h3>
        </div>

        <div class="readMore">
            @if(Session('username'))
                @foreach($fav_story as $row1)
                    @if($row1->story_id == $story_id)
                    <a href="{{ url('member/fav_story/'.$row->story_id) }}" style="background:#59AAE1;" > Unfavorite</a>
                    @elseif($story_id)
                        <a href="{{ url('member/fav_story/'.$row->story_id) }}" style="background:#1dbf73;" > Favorite</a>
                    @endif
                @endforeach
            @endif
        </div>
    </article>
@endforeach
@foreach($row形式的故事)
{{$row->story_desc};
{{$row->story_title}
@if(会话('username'))
@foreach($fav_story作为$row1)
@如果($row1->story\u id==$story\u id)
@elseif($story\u id)
@恩迪夫
@endforeach
@恩迪夫
@endforeach
标记为收藏夹的故事将显示一个带有该特定故事的不利按钮,再次单击该按钮将使其成为收藏夹。问题是,它会显示带有特定故事的“收藏”和“不收藏”按钮,而不会显示“收藏”按钮,需要此按钮才能使其成为收藏。

试试这个

在控制器中修改查询:

// this will store all story ids in an array
$data['fav_story'] = DB::table('favorite_story')->where('user_id', $user_id)->pluck('story_id');
您的刀片视图

...
<div class="readMore">
    @if(Session('username'))
       @if(in_array($row->story_id, $fav_story))
           <a href="{{ url('member/fav_story/'.$row->story_id) }}" style="background:#59AAE1;" > Unfavorite</a>
       @else
           <a href="{{ url('member/fav_story/'.$row->story_id) }}" style="background:#1dbf73;" > Favorite</a>
       @endif
    @endif
</div>
...
。。。
@if(会话('username'))
@if(在数组中($row->story\u id,$fav\u story))
@否则
@恩迪夫
@恩迪夫
...
编辑


更新
$row->id
$row->story\u id

给每个按钮一个不同的
id
,例如
id=“favorite-post1”
,然后反vorite按钮将是相同的
id=“反vorite-post1”
,并对所有其他帖子继续相同的过程。您也可以只为按钮提供它在DB中所属的post的
自动递增的
ID。因此,如果post 1的
自动递增的
ID为1,那么它将获得
ID=“1”
并继续。实际上,所有的post/stories都是动态的,将由不同的用户添加。每个用户都可以选择喜爱的故事。我只是对foreach循环感到困惑,如果您看到我在这里提到的视图代码。我有两张分开的桌子。故事的第一个tbl和最喜欢的故事的第二个tbl。在这两个表中,story_id是公共字段。
会话('username')
中是否有任何值?您能为我添加
$data['fav_story']
吗?是的,有一个会话(“用户名”)供登录的用户使用。因此,如果某个特定的登录用户已将某个特定的故事标记为收藏夹,则按钮应看起来不受欢迎,否则,登录用户未标记为收藏夹的故事,则按钮应显示为收藏夹。简单地说,对于facebook这样的特定用户,每个帖子的“喜欢/不喜欢”按钮都是一样的。希望你能理解我的问题