Welcome Guest, Not a member yet? Register   Sign In
Quick view question
#1

[eluser]tim1965[/eluser]
Hi

This is maybe a more general PHP question. But i am hoping some one can help with the right approach.
I have a view that displays the results of a query. My model returns this to the Controller as an array. I then echo the individual DB fields in a seperate view.
My problem is that i want to add something like.,  to a certain set of variables. However the query may or may not return these variables.
All i am trying to acheive here is to add 2 spaces and a comma, if the variable is populated for layout purposes.
So an example would be
<?php echo $dishwasher.',  '; ?>
in my view. This example obviously adds the spaces and comma´s irrespective of whether the query returns a result for that variable.

So the only way i can think to handle these currently is by manipulating the elements of the array before passing these to the view. But there must be an easier way.
Thanks in advance.
#2

[eluser]TheFuzzy0ne[/eluser]
Create a helper.
Code:
function nbsp_pad($var, $pad_count=2)
{
    if (isset($var))
    {
        return $var . str_repeat(' ', $pad_count);
    }
    return '';
}
The above code is untested.

You can call it from within your view like this:
Code:
<?php echo nbsp_pad($diswasher); ?>

However, I must ask why you want to add 2 nbsps to your code. Is this not something that can be done better with CSS?
#3

[eluser]tim1965[/eluser]
Fuzzyone

There probably is, and this is all about layout. But i only want this applied if the variable is populated, and i couldnt think of a way to associate applying CSS only if the variable is populated.
#4

[eluser]xwero[/eluser]
TheFuzzy0ne function will break if the variable is undefined, try
Code:
function isset_wrapper($var) { return isset($var); }
var_dump(isset_wrapper($not_existing));
The function will work if you pass the variable name as a string
Code:
function isset_wrapper($var_name) { return isset(${$var_name}); }
var_dump(isset_wrapper('not_existing'));

But i have to agree with TheFuzzy0ne this should be handled by css rather than adding html entities.
#5

[eluser]xwero[/eluser]
can you show us your view file?
#6

[eluser]tim1965[/eluser]
Ok

My actual view is very large and complex, so i will post an example.
I am trying to acheive a comma and 2 spaces after i echo the variable, but i only want it applied if the variable is populated

View
<div class="sub_header">Header</div>
<div class="sub_text">&lt;?php echo $test1.',&nbsp;&nbsp;'; ?&gt;&lt;?php echo $test2.',&nbsp;&nbsp; ?&gt;</div>
The effect i am trying to acheive is
Quote:
test1, test2, if both variables are returned by the query
test1, if only test 1 is returned by the query
I pass the array returned from my model to the view
$data=$this->model->get_property_details();
$this->load->view('view', $data);
As i said i am not aware of how i could acheive this in CSS.
Thanks in advance for you help
#7

[eluser]xwero[/eluser]
I assume the variables are strings otherwise you will have php errors
Code:
implode(',&nbsp;&nbsp;',array_filter(array($test1,$test2)))
array_filter will remove the empty strings and the values will be glued together by implode
#8

[eluser]tim1965[/eluser]
Ok so i have to manipulate the array before passing it to the view. Many thanks for everyones help.
#9

[eluser]xwero[/eluser]
You could do it in the loop then you don't have to go into the controller if you need to change the markup
Code:
&lt;?php foreach($rows as $row):
$subtext = implode(',&nbsp;&nbsp;',array_filter(array($row->test1,$row->test2))) ?&gt;
<div class="sub_header">Header</div>
<div class="sub_text">&lt;?php echo $subtext ?&gt;</div>
&lt;?php endforeach ?&gt;
#10

[eluser]tim1965[/eluser]
Just in case anybody else has this issue, it can be solved using CSS. What i was trying to generate was a horizontal list. See this article for a solution to my problem http://www.alistapart.com/articles/taminglists/

Apologies for posting a CSS problem here, and many thanks for everybodys contribution.




Theme © iAndrew 2016 - Forum software by © MyBB