0

On this test page https://wintoweb.com/sandbox/question_2.php , the visitor can make searches in the DB and tick as many checkboxes as wished. When button [Accept...] is clicked, I want the result of all searches to be shown under 'Your selections so far'. Right now, only the last search is displayed. I tried using a global array to store the result of previous searches and increment it upon each new one. That's where I have a problem.

On top of file I have :

<?php
    global $all_authors;
    array ($all_authors, '');
?>

At bottom of file I have :

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

if(isset($_GET['search'])){
    //echo 'Search</br>';
} elseif(isset($_GET['display_this'])) {
    echo getNames();
}

function getNames() {
    $rets = '';
    if(isset($_GET['choices']) and !empty($_GET['choices'])){
      foreach($_GET['choices'] as $selected){
        $rets .= $selected.' -- ';
      }
//array_push($all_authors, $rets); // This is the problem
//print_r($allAuthors); // this too
echo '</br><b>Your selections so far :</b></br>';
    }
    return $rets;
}
?>

EXPECTED: Results of all previous searches to be listed ACTUAL: No go due to problem with array_push(). See function gatNames()

stressless
  • 13
  • 4
  • You should try and re-write it without global variables, it's much more flexible and easier to test. You can usually do this by passing any required fields to/from any functions used. – Nigel Ren Aug 12 '19 at 09:49
  • Hmmm... I think you over estimate me. Which "required field" ? Can you hint me with a couple of line of code or more info? Thanks. – stressless Aug 12 '19 at 10:00

2 Answers2

0

You should make the array global inside the function, so on top:

$all_authors = array();

In the bottom:

function getNames() {
    global $all_authors;

    // Do the rest of the stuff
}
Paul
  • 329
  • 1
  • 8
0

You are returning $rets from your getNames function but not using it. You just need to use this variables $rets instead of Global Variable.


if(isset($_GET['search'])){
    //echo 'Search</br>';
} elseif(isset($_GET['display_this'])) {
    $rets = getNames(); //The $rets will hold the value returned by your function getName(). 
    if( !empty ( $rets ) ) {
       echo '</br><b>Your selections so far :</b></br>';
       echo $rets;
    }
}

You could remove the echo statement from inside your getNames Method.

function getNames() {
    $rets = '';
    if(isset($_GET['choices']) and !empty($_GET['choices'])){
      foreach($_GET['choices'] as $selected){
        $rets .= $selected.' -- ';
      }
    }
    return $rets;
}
ascsoftw
  • 3,466
  • 2
  • 15
  • 23
  • Thanks. Your solution does not remember the accepted results from past searches. e.g. Say user searches for 'dum' and checks DUMAS.Then searches for 'z' and checks ZOLA. I want DUMAS... -- ZOLA... to display under 'Your selections so far :'. for now, we only see the accepted result of each individual search. Reasons why I was trying to store past searches in an array. – stressless Aug 12 '19 at 11:42
  • @stressless This would not be possible using Global Arrays as well. User is searching for 2 Search Terms in 2 different HTTP Request. HTTP is a stateless protocol. So if you want to display such data to user, you will need to store this data in Session on every Search. So if user searches for 'dum', you save that in DB. Next Time User searches fro 'z', you save that in DB as well and then in your getNames() get the values from DB to display to user. – ascsoftw Aug 12 '19 at 13:10
  • I understand. I tried using $_SESSION with the same result. Alike globals, the session seems to be emptied upon every new search. I'm surprised because I thought sessions were stored on the server until destroyed programmatically or on timeout. I will try to store search results in a mysql table then. – stressless Aug 12 '19 at 15:44
  • Session should work. Would you update you code using Session. – ascsoftw Aug 13 '19 at 04:18
  • Good morning. To avoid confusion I have asked another question about session here :https://stackoverflow.com/questions/57472477/session-seems-to-retain-only-last-value – stressless Aug 13 '19 at 06:59
  • @stressless I have provided my answer there. – ascsoftw Aug 13 '19 at 07:02