2

Need to bind Page drop-down conditionally on base of 'Content' table. Page titles are stored in an associative array and 'Content' table have page code stored in it. Here is the code

Function which return page titles

    public function getPageTitles(){    
     $pageTitles = array("Home"=> "Home", 
              "AboutUs"=> "About Us", //AboutUs will save in database as pageCode
              "Features"=> "Features",
              "ContactUs"=> "Contact Us");

     return $pageTitles;
  }

Function which checks if page have content or not:

public function getPageTitlesWithNoContent()
{
    $pageTitles = $this->getPageTitles();

    $this->db->distinct('pageCode');
        $this->db->select('pageCode');
        $this->db->from('content');
    $this->db->where('status', 1);      
    $data = $this->db->get();

    $queryResult = $data ? $data->result_array() : 0 ;

    $emptyPageTitle = array();

    foreach($pageTitles as $x => $x_value)
    {
        $hasContent = in_array($x, $queryResult);

        if (!$hasContent){ 
         $emptyPageTitle[$x] = $x_value;
        }
    }

    return $emptyPageTitle;
}

This function is returning all page titles.. new to php no idea what is wrong

5
  • 2
    What's the print result of $queryResult? Commented Sep 27, 2018 at 16:34
  • well how to print it? .. first time working on php code .. not even able to setup a debugger :( Commented Sep 27, 2018 at 16:35
  • 1
    print_r($queryResult); Commented Sep 27, 2018 at 16:39
  • when I used this .. browser is not able to open the page "This site can’t be reached The webpage at localhost/pages might be temporarily down or it may have moved permanently to a new web address." Commented Sep 27, 2018 at 16:46
  • 1
    @TamilvananN print result is "Array ( [0] => Array ( [key] => Home ) [1] => Array ( [key] => ContactUs ) )" .. ok so I need to compare with value . .will try Commented Sep 27, 2018 at 19:18

3 Answers 3

2

Check name fields in table is same? With Uppercase first char?

Also change your code in this loop:

foreach($pageTitles as $x => $x_value)
{
    if (in_array($x, $queryResult)){ 
     $emptyPageTitle[$x] = $x_value;
    }
}

I remove ! negative in check condition

Sign up to request clarification or add additional context in comments.

4 Comments

sorry .. this comment is not clear to me "Check name fields in table is same? With Uppercase first char?" ..
$pageTitles = array("Home"=> "Home", "AboutUs"=> "About Us", //AboutUs will save in database as pageCode "Features"=> "Features", "ContactUs"=> "Contact Us"); in this array your list name of fields. This names also in you table structure? They are the same? With what do you compare this array?
Provide values of array $queryResult which data you get from query.
Array ( [0] => Array ( [key] => Home ) [1] => Array ( [key] => ContactUs )
1

@NMathur I think you almost got it. Made some changes for you in that code, Check it.

public function getPageTitlesWithNoContent() {

    $pageTitles = $this->getPageTitles();

    $this->db->select('pageCode');
    $this->db->from('content');
    $this->db->where('status', 1); 
    $this->db->group_by('pageCode');      
    $query = $this->db->get();

    $queryResult = array();

    foreach ($query->result_array() as $row) { // This loop should need to form an array based on query result
       $queryResult[$row['pageCode']] = $row['pageCode'];
    }

    $emptyPageTitle = array_diff_key($pageTitles,$queryResult); // Compares the keys from array1 against the keys from array2 and returns the difference

    return $emptyPageTitle;

}

1 Comment

Thank you so much for your help and guidance .. I learnt a lot
1

As @TamilvananN guided, I printed the queryResult and tried this workaround:

    foreach($pageTitles as $x => $x_value)
        {
            foreach ($queryResult as $item) 
            {
                if (!($x == $item['pageCode'])){ 
                 $emptyPageTitle[$x] = $x_value;
                }
            }
        }

It is working, but as you can see this has loop in a loop .. that can be very costly .. can you please share any fast way to compare the results.

Comments

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.