Hall of Famer Posted September 26, 2011 Share Posted September 26, 2011 Well I have a code that looks like this: <div style='width:100%; text-align:center; float:left;'>{$array[0][0]}</div> <div style='width:50%; text-align:center; float:left;'>{$array[1][0]}</div> <div style='width:25%; text-align:center; float:left;'>{$array[2][0]}</div> <div style='width:12.5%; text-align:center; float:left;'>{$array[3][0]}</div> As you can see, the width percentage decreases by half as the first index of $array increases by 1. I was wondering if its possible to write a PHP code such that the width percentage is automatically adjusted based on the first index of array. I've tried the following way as below, but it wouldnt work: <div style='width:{100/(2^$i)}%; text-align:center; float:left;'>{$array[$i][$j]}</div> So do anyone of you know how to do this? Please help. Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/ Share on other sites More sharing options...
requinix Posted September 27, 2011 Share Posted September 27, 2011 ^ does not do what you think it does. Use pow. Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273039 Share on other sites More sharing options...
Hall of Famer Posted September 27, 2011 Author Share Posted September 27, 2011 umm are you sure the above codes will work if I use pow instead of ^? It looks like this is causing the trouble: width:{100/(2^$i)}%; Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273108 Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 Try this width:{(100/pow(2,$i))}%; Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273111 Share on other sites More sharing options...
Hall of Famer Posted September 27, 2011 Author Share Posted September 27, 2011 umm looks like it wont work, I am assuming it is impossible to write an expression to specify the width percentage? The following code works perfectly: <div style='width:25%; text-align:center; float:left;'>{$array[2][0]}</div> But this one wont: <div style='width:{(100/pow(2,2))}%; text-align:center; float:left;'>{$array[2][0]}</div> Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273115 Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 My bad. <div style='width:".(100/pow(2,2))."%; text-align:center;'> Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273116 Share on other sites More sharing options...
Hall of Famer Posted September 27, 2011 Author Share Posted September 27, 2011 Thank you so much, it works. And btw, can I resize images based on the same mechanism? <img src='{$row['avatar']}' border=0 width='50' height='25' /> Can be converted into this? <img src='{$row['avatar']}' border=0 width=".(100/pow(2,1))." height=".(100/pow(2,2))." /> Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273124 Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 Cant see why not Try it and see. Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273125 Share on other sites More sharing options...
Hall of Famer Posted September 27, 2011 Author Share Posted September 27, 2011 Alright I will give a try. I wonder if concatenation operator is needed for the img src code since there is already an assignment operator = here. Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273127 Share on other sites More sharing options...
requinix Posted September 27, 2011 Share Posted September 27, 2011 I am assuming it is impossible to write an expression to specify the width percentage? "expressions" inside strings only work if you start with a $ (like with a variable). There are creative ways around this though: $expr = function($value) { return $value; }; // or use create_function() $string = "{$expr(100/4)}%"; Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273272 Share on other sites More sharing options...
Hall of Famer Posted September 28, 2011 Author Share Posted September 28, 2011 Thank you so so much, I've got it to work already. Your help is much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/247918-about-in-php-please-help/#findComment-1273435 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.