Keith Power
Keith Power

Reputation: 14151

cakephp view will not display

I have an order form to purchse a voucher but for some reason now when I click phurcase it displays a blank screen. I cant see what has changed and why the view is not being passed the information from the controller.

Vourchers form

        <h2>Vouchers</h2>

        <?php   
                foreach($vouchers as $v){
        ?>


            <div id="voucher_box" class="add_shadow column span-8">

                <h3><?=$v['Voucher']['title'];?></h3>

                <p>
                    <?=$v['Voucher']['description'];?>
                </p>

            <?php 
                echo $this->Form->create('Voucher',array('id'=>'ResVoucherForm','url'=>'/res/voucher'));
                echo $this->Form->input('slug',array('type'=>'hidden','value'=>$v['Voucher']['slug']));
            ?>  

                <select id="VoucherPrice" name="data[Voucher][price]">
                <? $prices = explode(',',$v['Voucher']['prices'])?>

                <? foreach($prices as $price){?>
                    <option value="<?=$price?>">&euro; <?=$price?></option>
                <? } ?>
                </select>

                <div class="submit"><input type="submit" id="check_rates" value="Purchase this" class="ui-button ui-widget ui-state-default ui-corner-all" /></div>

                </form>

            </div>



        <?
                }
        ?>

Controller

function voucher() {
    $this->layout = '360';

    $msg[0] = array(); // 0 = bad messages
    $msg[1] = array(); // 1 = good messages

    $total_price = 0.00;

    if(isset($data['Voucher'])) { $this->data['Voucher'] = $data['Voucher']; }

    if(isset($this->data['Voucher'])) {

        // if we have posted a voucher purchase: add it to session array
        if(isset($this->data['Voucher']['slug'])){

            $slug = $this->data['Voucher']['slug'];
            $voucher = $this->getVoucher($slug);

            $voucher['Voucher']['price'] = $this->data['Voucher']['price'];

            $vouchers = $this->Session->read('Res.Vouchers'); // read existing voucher orders

            if(is_array($vouchers) && isset($voucher['Voucher'])){

                $temp['id']             = $voucher['Voucher']['id'];
                $temp['title']          = $voucher['Voucher']['title'];
                $temp['description']    = $voucher['Voucher']['description'];
                $temp['slug']           = $voucher['Voucher']['slug'];
                $temp['price']          = $this->data['Voucher']['price'];

                $vouchers[] = $temp;
            }

            $this->Session->write('Res.Vouchers',$vouchers);
        } else {
            $vouchers = $this->Session->read('Res.Vouchers'); // read existing voucher orders
        }   

        $this->set('voucher_orders', $vouchers);
    }

This next view displays blank, I do not know how to test the information in the controller

            <?php
            /*
                if voucher show
            */

                if (isset($voucher)) { 

            ?>
                    <div id="voucher_box" class="add_shadow column span-8">

                        <h3><?=$voucher['Voucher']['title'];?></h3>

                        <p>
                            <?=$voucher['Voucher']['description'];?>
                        </p>

                    <?php 
                        echo $this->Form->create('Voucher',array('id'=>'ResVoucherForm','url'=>'/res/voucher'));
                        echo $this->Form->input('slug',array('type'=>'hidden','value'=>$voucher['Voucher']['slug']));
                    ?>  

                        <select id="VoucherPrice" name="data[Voucher][price]">
                        <? $prices = explode(',',$voucher['Voucher']['prices'])?>

                        <? foreach($prices as $price){?>
                            <option value="<?=$price?>">&euro; <?=$price?></option>
                        <? } ?>
                        </select>

                        <div class="submit"><input type="submit" id="check_rates" value="Purchase this" class="ui-button ui-widget ui-state-default ui-corner-all" /></div>

                        </form>

                    </div>
            <?
                } 
                // end if voucher show
            ?>

Upadate Error Returned #######################################################

Notice (8): Undefined index: id [APP/controllers/res_controller.php, line 739]

Notice (8): Undefined index: title [APP/controllers/res_controller.php, line 740]

Notice (8): Undefined index: description [APP/controllers/res_controller.php, line 741]

Notice (8): Undefined index: slug [APP/controllers/res_controller.php, line 742]

Array ( [0] => Array ( [id] => [title] => [description] => [slug] => [price] => 100 )

)

Upvotes: 0

Views: 1075

Answers (3)

Keith Power
Keith Power

Reputation: 14151

Thanks for all your help. By using the pr() or debug() in the controller I found the problem. It was in the function that was getting called on the line $voucher = $this->getVoucher($slug);. I now have a better understanding of cakephp.

Upvotes: 1

8vius
8vius

Reputation: 5836

You are setting 'voucher_orders' to your view not 'voucher' the name of the variable in the view is always the string passed in the set method. Also I don't quite understand what you are doing in the controller with the $data variable, where does it come from? Also it's redundant doing $this->data['Voucher'] = $data['Voucher'] and the checking $this->data isset if you already checked if $data is set. You might be overwriting your $this->data array, what you are doing is not necessary since the view already gives you a populated $this->data if its a form submission.

You should really try doing some tutorials and reading up a little before jumping in, you seem to not properly understand the CakePHP structure and workflow.

Upvotes: 1

bfavaretto
bfavaretto

Reputation: 71939

In the view, you have this around the whole HTML:

if (isset($voucher)) {
    // ...
}

Meaning: display voucher information if there is a voucher, and do not display anything otherwise.

Now, in your controller, you don't seem to be passing a voucher variable to the view (which should be done with $this->set('varname', $var);

So, your view does not get any data, thus it does not display anything.

This could explain your issue. But if the screen is completely blank (not even the layout displays), then you must enable error logging to check what's going on.

Upvotes: 2

Related Questions