0

I have an array of n elements and I want to bind it with the value of $genre. This is what I've tried: $genre = $_GET['a_gen'] ?? ''; The $genre is from a previous query and it is a string. $aTitle is also an array of strings.

<?php
                    if (isset($_POST['submit-search'])) {
                        $stmt = $conn -> prepare("SELECT a_titlu, a_autor, a_text, a_data, a_imagine, a_gen FROM articol WHERE a_titlu LIKE ? OR a_text LIKE ? 
                            OR a_autor LIKE ? OR a_data LIKE ?");
                        $search ='%'.$_POST['search'].'%';
                        $stmt -> bind_param('ssss', $search, $search, $search, $search);
                        $stmt -> execute();
                        $stmt -> store_result();
                        $stmt -> bind_result($a_titlu, $a_autor, $a_text, $a_data, $a_imagine, $a_gen);
                        $stmt -> get_result();
                        $count = $stmt -> num_rows();
                        $genre = $_GET['a_gen'] ?? '';
                        $aTitle = array();
                        if ($count > 0) {
                            if($count == 1)
                                echo "<h5 class='text-center toateArt'>Am găsit ".$count." rezultat!</h5>";
                            else
                                echo "<h5 class='text-center toateArt'>Am găsit ".$count." rezultate!</h5>";
                            while ($stmt -> fetch()) {
                                array_push($aTitle, $a_titlu);
                                echo "<div class='card'>
                                        <img src=".$a_imagine." class='imgRes' />
                                        <h3 class='articleTitle'>".$a_titlu."</h3>
                                        <h6 class='articleDT'><i class='fa fa-calendar'></i>".$a_data."&nbsp<i class='fa fa-user'></i>".$a_autor."</h6>
                                        <p class='articleDescription' style='text-align: justify;'>".$a_text."</p>
                                        <div class='button_cont' align='right'>
                                            <a class='buttonA' style='text-decoration: none;' href='article.php?title=".$a_titlu."&date=".$a_data."'>Citește</a>
                                        </div>
                                    </div>
                                <br>";
                            }
                        }
                    }
                ?>
                <?php
                    $in = str_repeat('?,', count($aTitle) - 1) . '?';
                    $aTitle[] = $genre;
                    $stmt = $conn -> prepare("SELECT  a_titlu, a_data, a_gen FROM articol WHERE a_titlu NOT IN ($in) AND a_gen = ?");
                    $types = str_repeat('s', count($aTitle));
                    $stmt -> bind_param($types, ...$aTitle);
                    $stmt -> execute();
                    $stmt -> store_result();
                    $stmt -> bind_result($a_titlu, $a_data, $a_gen);
                    $stmt -> get_result();
                    $count = $stmt -> num_rows();

                        while ($stmt -> fetch()) {
                            echo "<div class='card'><p>".$a_titlu."</p><p>".$a_data."</p><p>".$a_gen."</p></div><br>";
                        }
                ?>
4
  • you need a type for genre too Commented Apr 27, 2020 at 18:38
  • $genre is from a previous query and it is a string Commented Apr 27, 2020 at 18:40
  • sigh*. $types . = 's'; Commented Apr 27, 2020 at 18:42
  • What is this $aGenre = ('s' $genre)? Commented Apr 27, 2020 at 18:48

1 Answer 1

1

As you can't put in an additional parameter after using the argument unpacking on $aTitle, so the easiest way would be to add this into the array at the end.

$in = str_repeat('?,', count($aTitle) - 1) . '?';
// Now add in value
$aTitle[] = $genre;
$stmt = $conn -> prepare("SELECT  a_titlu, a_data, a_gen 
                          FROM articol 
                          WHERE a_titlu NOT IN ($in) AND a_gen = ?");
$types = str_repeat('s', count($aTitle));
$stmt -> bind_param($types, ...$aTitle);
Sign up to request clarification or add additional context in comments.

3 Comments

I've checked and it doesn't add the $genre at the final of the array
Try checking $aTitle before the $aTitle[] = $genre; and after.
It works now! It seems that the $_GET['a_gen'] was empty and that's why it doesn't work. I've just added like this: $genre = $a_gen; thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.