How to create a function to use an array in the function to get a result

How does the function work? How do I create a function which I could insert an array ($keyword_color1[0, $keyword_color1[1]], $keyword_color1[2]…) to get a result?

<?php 
//for overview.php
$bk2=13;
$ch2=10;

//$keyword_color1 = array(array("Asshur","darkgreen"), array("Assyria","darkgreen"), array("kingdom","red"), array(" nation","darkolivegreen"));
//$keyword_color1 = array(array("escape","navy"), array("Israel","darkslategray"), array("Jacob","indigo"), array("people","darkred"), array("prince","olive"));
//$keyword_color1 = array(array(" nation","darkolivegreen"), array("Israel","darkslategray"), array("Jacob","indigo"), array("people","darkred"), array("prince","olive"));
//$keyword_color1 = array(array("charge","yellowgreen"), array("give","sienna"), array("widow","sandybrown"));
//$keyword_color1 = array(array("widow","sandybrown"));

$keyword_color1 = array(array("flee","pink"), array("fled","pink"), array("Saul","teal"));

//$keyword_color1 = array(array("kingdom","red"), array("hand","blue"));

//$keyword_color1 = array(array("Saul","teal"), array("Gibeah","lime"));
//$keyword_color1 = array(array("escape","navy"));
  //function give($array) {
    $sql9Part = "SELECT * FROM ".$dbTable3." WHERE ";
    //$sql9a .= $sql9Part."(";
    $sql9a = $sql9Part."(book = '" .$b1. "' AND chapter= '" .$c1. "') ";
    $sql9b = $sql9Part."(book = '" .$bk2. "' AND chapter= '" .$ch2. "') ";
    //$sql9 .= ") ";
    
    
    
    
    if(count($keyword_color1) != 0 ){
    $sql9 = "AND (";
    $sql9a .= $sql9;              
    $sql9b .= $sql9;
        for($j=0; $j < count($keyword_color1);$j++){
            if($j>0 && $j > count($keyword_color1)-2){
                //$kwstring .= "<br />+ ";
                //$wordsfound .= " and \n";
                $sql9a .= " OR ";              
                $sql9b .= " OR ";
            }
            //$kwstring .= $keyword_color1[$j][0];
            //$sql9a .= "text_data LIKE '%".$keyword_color1[$j][0]."%'";
            $sql9a .= "text_data LIKE '%".$keyword_color1[$j][0]."%'";
            //$sql9b .= "text_data LIKE '%".$keyword_color1[$j][0]."%'";
            $sql9b .= "text_data LIKE '%".$keyword_color1[$j][0]."%'";
            
            //$wordsfound .= "<span style=\"font-weight: bold; color: ".$keyword_color1[$j][1]."\">".$keyword_color1[$j][0]."</span>";
            if($j < count($keyword_color1)-2){
                //$kwstring .= "<br />+ ";
                //$wordsfound .= ", \n";
                $sql9a .= " OR ";              
                $sql9b .= " OR ";
            }
        }
        $sql9a .= ")";
        $sql9b .= ")";
    }
        //$wordsfound .= ". \n";
    
        $sql9a .= " ORDER BY id ASC";
        $sql9b .= " ORDER BY id ASC";
        // echo $sql9a."<br />\n";
        // echo $sql9b."<br />\n";
    
        // Step 3: Send the query
        $result9a = $db->query($sql9a);
        $result9b = $db->query($sql9b);
        // Step 4: Iterate over the results
        while($row9a = $result9a->fetch(PDO::FETCH_ASSOC)) {
        $id9a[] = $row9a['id'];
        $bookSpoke9a[] = $row9a['book_spoke'];
        $chapterSpoke9a[] = $row9a['chapter_spoke'];
        $verseSpoke9a[] = $row9a['verse_spoke'];
        $bookTitle9a[] = $row9a['book_title'];
        $book9a[] = $row9a['book'];
        $chapter9a[] = $row9a['chapter'];
        $verse9a[] = $row9a['verse'];
        $recordType9a[] = $row9a['recordType'];
        $textData9a[] = $row9a['text_data'];
        $bSpokeCycle9a[] = $row9a['b_spoke_cycle'];
        $cSpokeCycle9a[] = $row9a['c_spoke_cycle'];
        $vSpokeCycle9a[] = $row9a['v_spoke_cycle'];
    }
    while($row9b = $result9b->fetch(PDO::FETCH_ASSOC)) {
        $id9b[] = $row9b['id'];
        $bookSpoke9b[] = $row9b['book_spoke'];
        $chapterSpoke9b[] = $row9b['chapter_spoke'];
        $verseSpoke9b[] = $row9b['verse_spoke'];
        $bookTitle9b[] = $row9b['book_title'];
        $book9b[] = $row9b['book'];
        $chapter9b[] = $row9b['chapter'];
        $verse9b[] = $row9b['verse'];
        $recordType9b[] = $row9b['recordType'];
        $textData9b[] = $row9b['text_data'];
        $bSpokeCycle9b[] = $row9b['b_spoke_cycle'];
        $cSpokeCycle9b[] = $row9b['c_spoke_cycle'];
        $vSpokeCycle9b[] = $row9b['v_spoke_cycle'];
    }
    //var_dump($recordType9a);
    //var_dump($recordType9b);
    
    echo $h3; 
    
    // for the first text
    $resultFounda = "";
    for($i=0; $i < count($recordType9a); $i++){
        $resultFounda .= "<span style=\"font-size: smaller; font-weight: bold; vertical-align: text-top;\">".$verse9a[$i]."</span> ";
        $strTexta = stripslashes($textData9a[$i]);
        for($j=0; $j<count($keyword_color1);$j++){        
                $strTexta = preg_replace("/(".$keyword_color1[$j][0].")/i", "<span class=\"\" id=\"\" style=\"color: ".$keyword_color1[$j][1]."; font-weight: bold;\">$1</span>", $strTexta);
        }
        $resultFounda .= $strTexta."<br />\n";
    }
    
    
    // for the second text
    $resultFoundb = "";
    for($i=0; $i < count($recordType9b); $i++){
        $resultFoundb .= "<span style=\"font-size: smaller; font-weight: bold; vertical-align: text-top;\">".$verse9b[$i]."</span> ";
        $strTextb = stripslashes($textData9b[$i]);
        for($j=0; $j<count($keyword_color1);$j++){        
                $strTextb = preg_replace("/(".$keyword_color1[$j][0].")/i", "<span class=\"\" id=\"\" style=\"color: ".$keyword_color1[$j][1]."; font-weight: bold;\">$1</span>", $strTextb);
        }
        $resultFoundb .= $strTextb."<br />\n";
    }
    
    $h4 = "";
    for($i=0; $i < count($keyword_color1); $i++){
        if($i>0 && $i>(count($keyword_color1)-2)){
            $h4 .= " and ";
        }else{
            if($i>0){
                $h4 .= ", ";
            }
        }
        $h4 .= ucfirst($keyword_color1[$i][0]);
    }
    //return $result;
  //}
/***************************************************************************************************
  //function give($array) {
    $sql9Part = "SELECT * FROM ".$dbTable3." WHERE ";
    //$sql9a .= $sql9Part."(";
    $sql9a = $sql9Part."(book = '" .$b1. "' AND chapter= '" .$c1. "') ";
    $sql9b = $sql9Part."(book = '" .$b2. "' AND chapter= '" .$c2. "') ";
    //$sql9 .= ") ";
    
    
    
    
    if(count($keyword_color2) != 0 ){
    $sql9 = "AND (";
    $sql9a .= $sql9;              
    $sql9b .= $sql9;
        for($j=0; $j < count($keyword_color2);$j++){
            if($j > count($keyword_color2)-2){
                //$kwstring .= "<br />+ ";
                //$wordsfound .= " and \n";
                $sql9a .= " OR ";              
                $sql9b .= " OR ";
            }
            //$kwstring .= $keyword_color2[$j][0];
            //$sql9a .= "text_data LIKE '%".$keyword_color2[$j][0]."%'";
            $sql9a .= "text_data LIKE '%".$keyword_color2[$j][0]."%'";
            //$sql9b .= "text_data LIKE '%".$keyword_color2[$j][0]."%'";
            $sql9b .= "text_data LIKE '%".$keyword_color2[$j][0]."%'";
            
            //$wordsfound .= "<span style=\"font-weight: bold; color: ".$keyword_color2[$j][1]."\">".$keyword_color2[$j][0]."</span>";
            if($j < count($keyword_color2)-2){
                //$kwstring .= "<br />+ ";
                //$wordsfound .= ", \n";
                $sql9a .= " OR ";              
                $sql9b .= " OR ";
            }
        }
        $sql9a .= ")";
        $sql9b .= ")";
    }
        //$wordsfound .= ". \n";
    
        $sql9a .= " ORDER BY id ASC";
        $sql9b .= " ORDER BY id ASC";
        // echo $sql9a."<br />\n";
        // echo $sql9b."<br />\n";
    
        // Step 3: Send the query
        $result9a = $db->query($sql9a);
        $result9b = $db->query($sql9b);
        // Step 4: Iterate over the results
        while($row9a = $result9a->fetch(PDO::FETCH_ASSOC)) {
        $id9a[] = $row9a['id'];
        $bookSpoke9a[] = $row9a['book_spoke'];
        $chapterSpoke9a[] = $row9a['chapter_spoke'];
        $verseSpoke9a[] = $row9a['verse_spoke'];
        $bookTitle9a[] = $row9a['book_title'];
        $book9a[] = $row9a['book'];
        $chapter9a[] = $row9a['chapter'];
        $verse9a[] = $row9a['verse'];
        $recordType9a[] = $row9a['recordType'];
        $textData9a[] = $row9a['text_data'];
        $bSpokeCycle9a[] = $row9a['b_spoke_cycle'];
        $cSpokeCycle9a[] = $row9a['c_spoke_cycle'];
        $vSpokeCycle9a[] = $row9a['v_spoke_cycle'];
    }
    while($row9b = $result9b->fetch(PDO::FETCH_ASSOC)) {
        $id9b[] = $row9b['id'];
        $bookSpoke9b[] = $row9b['book_spoke'];
        $chapterSpoke9b[] = $row9b['chapter_spoke'];
        $verseSpoke9b[] = $row9b['verse_spoke'];
        $bookTitle9b[] = $row9b['book_title'];
        $book9b[] = $row9b['book'];
        $chapter9b[] = $row9b['chapter'];
        $verse9b[] = $row9b['verse'];
        $recordType9b[] = $row9b['recordType'];
        $textData9b[] = $row9b['text_data'];
        $bSpokeCycle9b[] = $row9b['b_spoke_cycle'];
        $cSpokeCycle9b[] = $row9b['c_spoke_cycle'];
        $vSpokeCycle9b[] = $row9b['v_spoke_cycle'];
    }
    //var_dump($recordType9a);
    //var_dump($recordType9b);
    
    echo $h3; 
    
    // for the first text
    $resultFounda = "";
    for($i=0; $i < count($recordType9a); $i++){
        $resultFounda .= "<span style=\"font-size: smaller; font-weight: bold; vertical-align: text-top;\">".$verse9a[$i]."</span> ";
        $strTexta = stripslashes($textData9a[$i]);
        for($j=0; $j<count($keyword_color2);$j++){        
                $strTexta = preg_replace("/(".$keyword_color2[$j][0].")/i", "<span class=\"\" id=\"\" style=\"color: ".$keyword_color2[$j][1]."; font-weight: bold;\">$1</span>", $strTexta);
        }
        $resultFounda .= $strTexta."<br />\n";
    }
    
    
    // for the second text
    $resultFoundb = "";
    for($i=0; $i < count($recordType9b); $i++){
        $resultFoundb .= "<span style=\"font-size: smaller; font-weight: bold; vertical-align: text-top;\">".$verse9b[$i]."</span> ";
        $strTextb = stripslashes($textData9b[$i]);
        for($j=0; $j<count($keyword_color2);$j++){        
                $strTextb = preg_replace("/(".$keyword_color2[$j][0].")/i", "<span class=\"\" id=\"\" style=\"color: ".$keyword_color2[$j][1]."; font-weight: bold;\">$1</span>", $strTextb);
        }
        $resultFoundb .= $strTextb."<br />\n";
    }
    
    $h4 = "";
    for($i=0; $i < count($keyword_color2); $i++){
        if($i>(count($keyword_color2)-2)){
            $h4 .= " and ";
        }
        $h4 .= ucfirst($keyword_color2[$i][0]);
    }
    //return $result;
  //} */
?>
<h4><?php 
echo $h4;
?></h4>
<p class="BVerse">
<?php echo $blblinks1; 
echo $resultFounda;
?>
<br />
<br />
<br />
<?php echo $blblinks2; 
echo $resultFoundb;

?>
</p>
<br />
<br />
<br />

basically you do it like that:

function doSomething(array $input)
{
    // do something with the input array to produce $result

    return $result;
}
1 Like

Ok in this case the $input should be
$bk2=13;
$ch2=10;
$keyword_color1 = array(array(“flee”,“pink”), array(“fled”,“pink”), array(“Saul”,“teal”));
But how will it be written in the brackets?

Somehow the array is going to be in the for loop whereas $b2 and $ch2 will be out of the loop of the function. But this is what i’m not sure. Is the for loop going to be around the actual coding of the function or only when I call the function?

Where’s the values for $b1 $c1 and $dbTable coming from? If they’re coming from the user then they must be validated and a prepared statement used

$b1 and $c1 are from the url, whereas $db Table is from within. What will change is $keyword_color1 or $keyword_color which will be an array of arrays. For example the function is supposed to make a search of, let’s say an array of 3 words and 3 colors, then a second time the function searches an array of five words with their colors, then 2 words and colors…

How are you supposed to define them?

Notice: Undefined variable: dbTable3 in C:.…notes-isaiah10-1chronicles10.php on line 19

Notice: Undefined variable: b1 in C:.…notes-isaiah10-1chronicles10.php on line 21

Notice: Undefined variable: c1 in C:.…notes-isaiah10-1chronicles10.php on line 21

Notice: Undefined variable: bk2 in C:.…notes-isaiah10-1chronicles10.php on line 22

Notice: Undefined variable: ch2 in C:.…notes-isaiah10-1chronicles10.php on line 22

Notice: Undefined variable: keyword_color1 in C:.…notes-isaiah10-1chronicles10.php on line 25

Notice: Undefined variable: db in C:.…notes-isaiah10-1chronicles10.php on line 61

Fatal error: Call to a member function query() on null in C:.…notes-isaiah10-1chronicles10.php on line 61

Not sure how it would work out:

//for overview.php
$bk2=13;
$ch2=10;

//$keyword_color1 = array(array("Asshur","darkgreen"), array("Assyria","darkgreen"), array("kingdom","red"), array(" nation","darkolivegreen"));
//$keyword_color1 = array(array("escape","navy"), array("Israel","darkslategray"), array("Jacob","indigo"), array("people","darkred"), array("prince","olive"));
//$keyword_color1 = array(array(" nation","darkolivegreen"), array("Israel","darkslategray"), array("Jacob","indigo"), array("people","darkred"), array("prince","olive"));
//$keyword_color1 = array(array("charge","yellowgreen"), array("give","sienna"), array("widow","sandybrown"));
//$keyword_color1 = array(array("widow","sandybrown"));

$keyword_color1 = array(array("flee","pink"), array("fled","pink"), array("Saul","teal"));

//$keyword_color1 = array(array("kingdom","red"), array("hand","blue"));

//$keyword_color1 = array(array("Saul","teal"), array("Gibeah","lime"));
//$keyword_color1 = array(array("escape","navy"));
  function give() {
    $sql9Part = "SELECT * FROM ".$dbTable3." WHERE ";
    //$sql9a .= $sql9Part."(";
    $sql9a = $sql9Part."(book = '" .$b1. "' AND chapter= '" .$c1. "') ";
    $sql9b = $sql9Part."(book = '" .$bk2. "' AND chapter= '" .$ch2. "') ";
    //$sql9 .= ") ";
    
    if(count($keyword_color1) != 0 ){
    $sql9 = "AND (";
    $sql9a .= $sql9;              
    $sql9b .= $sql9;
        for($j=0; $j < count($keyword_color1);$j++){
            if($j>0 && $j > count($keyword_color1)-2){
                //$kwstring .= "<br />+ ";
                //$wordsfound .= " and \n";
                $sql9a .= " OR ";              
                $sql9b .= " OR ";
            }
            //$kwstring .= $keyword_color1[$j][0];
            //$sql9a .= "text_data LIKE '%".$keyword_color1[$j][0]."%'";
            $sql9a .= "text_data LIKE '%".$keyword_color1[$j][0]."%'";
            //$sql9b .= "text_data LIKE '%".$keyword_color1[$j][0]."%'";
            $sql9b .= "text_data LIKE '%".$keyword_color1[$j][0]."%'";
            
            //$wordsfound .= "<span style=\"font-weight: bold; color: ".$keyword_color1[$j][1]."\">".$keyword_color1[$j][0]."</span>";
            if($j < count($keyword_color1)-2){
                //$kwstring .= "<br />+ ";
                //$wordsfound .= ", \n";
                $sql9a .= " OR ";              
                $sql9b .= " OR ";
            }
        }
        $sql9a .= ")";
        $sql9b .= ")";
    }
        //$wordsfound .= ". \n";
    
        $sql9a .= " ORDER BY id ASC";
        $sql9b .= " ORDER BY id ASC";
        // echo $sql9a."<br />\n";
        // echo $sql9b."<br />\n";
    
        // Step 3: Send the query
        $result9a = $db->query($sql9a);
        $result9b = $db->query($sql9b);
        // Step 4: Iterate over the results
        while($row9a = $result9a->fetch(PDO::FETCH_ASSOC)) {
        $id9a[] = $row9a['id'];
        $bookSpoke9a[] = $row9a['book_spoke'];
        $chapterSpoke9a[] = $row9a['chapter_spoke'];
        $verseSpoke9a[] = $row9a['verse_spoke'];
        $bookTitle9a[] = $row9a['book_title'];
        $book9a[] = $row9a['book'];
        $chapter9a[] = $row9a['chapter'];
        $verse9a[] = $row9a['verse'];
        $recordType9a[] = $row9a['recordType'];
        $textData9a[] = $row9a['text_data'];
        $bSpokeCycle9a[] = $row9a['b_spoke_cycle'];
        $cSpokeCycle9a[] = $row9a['c_spoke_cycle'];
        $vSpokeCycle9a[] = $row9a['v_spoke_cycle'];
    }
    while($row9b = $result9b->fetch(PDO::FETCH_ASSOC)) {
        $id9b[] = $row9b['id'];
        $bookSpoke9b[] = $row9b['book_spoke'];
        $chapterSpoke9b[] = $row9b['chapter_spoke'];
        $verseSpoke9b[] = $row9b['verse_spoke'];
        $bookTitle9b[] = $row9b['book_title'];
        $book9b[] = $row9b['book'];
        $chapter9b[] = $row9b['chapter'];
        $verse9b[] = $row9b['verse'];
        $recordType9b[] = $row9b['recordType'];
        $textData9b[] = $row9b['text_data'];
        $bSpokeCycle9b[] = $row9b['b_spoke_cycle'];
        $cSpokeCycle9b[] = $row9b['c_spoke_cycle'];
        $vSpokeCycle9b[] = $row9b['v_spoke_cycle'];
    }
    //var_dump($recordType9a);
    //var_dump($recordType9b);
    
    echo $h3; 
    
    // for the first text
    $resultFounda = "";
    for($i=0; $i < count($recordType9a); $i++){
        $resultFounda .= "<span style=\"font-size: smaller; font-weight: bold; vertical-align: text-top;\">".$verse9a[$i]."</span> ";
        $strTexta = stripslashes($textData9a[$i]);
        for($j=0; $j<count($keyword_color1);$j++){        
                $strTexta = preg_replace("/(".$keyword_color1[$j][0].")/i", "<span class=\"\" id=\"\" style=\"color: ".$keyword_color1[$j][1]."; font-weight: bold;\">$1</span>", $strTexta);
        }
        $resultFounda .= $strTexta."<br />\n";
    }
    
    
    // for the second text
    $resultFoundb = "";
    for($i=0; $i < count($recordType9b); $i++){
        $resultFoundb .= "<span style=\"font-size: smaller; font-weight: bold; vertical-align: text-top;\">".$verse9b[$i]."</span> ";
        $strTextb = stripslashes($textData9b[$i]);
        for($j=0; $j<count($keyword_color1);$j++){        
                $strTextb = preg_replace("/(".$keyword_color1[$j][0].")/i", "<span class=\"\" id=\"\" style=\"color: ".$keyword_color1[$j][1]."; font-weight: bold;\">$1</span>", $strTextb);
        }
        $resultFoundb .= $strTextb."<br />\n";
    }
    
    $h4 = "";
    for($i=0; $i < count($keyword_color1); $i++){
        if($i>0 && $i>(count($keyword_color1)-2)){
            $h4 .= " and ";
        }else{
            if($i>0){
                $h4 .= ", ";
            }
        }
        $h4 .= ucfirst($keyword_color1[$i][0]);
    }
    return $result;
  }
  
  for($i=0; $i<count($keyword_color1); $i++){
    give($keyword_color1[$i]);
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.