Quick view question |
[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.
[eluser]TheFuzzy0ne[/eluser]
Create a helper. Code: function nbsp_pad($var, $pad_count=2) 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?
[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.
[eluser]xwero[/eluser]
TheFuzzy0ne function will break if the variable is undefined, try Code: function isset_wrapper($var) { return isset($var); } Code: function isset_wrapper($var_name) { return isset(${$var_name}); } But i have to agree with TheFuzzy0ne this should be handled by css rather than adding html entities.
[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"><?php echo $test1.', '; ?><?php echo $test2.', ?></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
[eluser]xwero[/eluser]
I assume the variables are strings otherwise you will have php errors Code: implode(', ',array_filter(array($test1,$test2)))
[eluser]tim1965[/eluser]
Ok so i have to manipulate the array before passing it to the view. Many thanks for everyones help.
[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: <?php foreach($rows as $row):
[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. |
Welcome Guest, Not a member yet? Register Sign In |