Search code examples
yii-extensionsyiiyii-components

render 2 index pages depend on query result - Yii framework


I need 2 index pages 1. index.php 2. index2.php

that render depend on if the query return back any result.

something like this:

    $sql = 'id = $id AND field LIKE ¥'%word%¥' ';

    $sql = 'id = $id and MATCH(field) AGAINST (¥'word' IN BOOLEAN MODE)';

    $query=$connection->createCommand($sql)->queryRow();

    if ($query== true) {
    $this->render('index1', array()
    } else {
    $this->render('index', array()
    }

Solution

  • You can do this. But it seems you are getting error in lines before rendering your view. The problem is here that you are writing bad string. In php when you want to add variable into string directly you must use double quotation instead of single.

    for example:

    $name="test";
    
    // RIGHT
    $test="hello $name";
    //WRONG
    $test='hello $name';
    

    if you want to add variable with a single quotation you must devide your string and variable by using dot(.);

    for example:

    $name="test";
    $test='hello '.$name;
    

    So this is the reason you give an error before rendering your views.