Welcome Guest, Not a member yet? Register   Sign In
how to pass variiables from view to controller ??
#1

[eluser]aamiraziz[/eluser]

Respected helpers,

i am working on job portal. i have a problem in showing job details.

i displayed all the Jobs with few attributes of job table on one page for specific user (who has posted that jobs), and provide a link that if user like to view complete detail. i have written code:

<?php
echo "<tr>";
echo "<td> <b> Job Title </b> </td>";
echo "<td> <b> Job Department </b> </td>";
echo "<td> <b> Job Type </b> </td>";
echo "</tr>";

$i = 1;
foreach ($myjobs as $ti => $a){
echo "<tr>";
echo "<td>";
echo $a['title'];
echo "</td>";

echo "<td>";
echo $a['Dpt'];
echo "</td>";

echo "<td>";
echo $a['Type'];
echo "</td>";


$id[$i] = $a['JobID'];
$x= $id[$i];
$i++;



echo "<td>";

echo "<li class='register'><a href=";
echo site_url('jbcontroller/showjob/$x');

/* wrong page redirection $x is not allowed only value is allowed
i need to pass a variable here, in which JobID is stored so that i can retrieve it from database.
1) how can i do that ??
2) is there any alternate logic to to this type of task ??
please help me i'll be very thank full to you*/

echo">Show Job Detail</li>";

echo "</td>";
echo "</tr>";

echo "<br />";
echo "<br />";
}
?&gt;




pastebin link: http://pastebin.com/m50dff433[size=3]
[/size]
#2

[eluser]tonanbarbarian[/eluser]
Your issue here is purely syntax
change
<code>echo site_url('jbcontroller/showjob/$x');</code>
to
<code>echo site_url('jbcontroller/showjob/'.$x);</code>

would also work if you change the single quotes to double quotes.
PHP does not evaluate variables in a string if the string is defined with single quotes, only if the string uses double quotes

Historically PHP has always been recommended to use single quotes rather than double quotes because if you use double quotes PHP has to parse the string to see if there are any variables to evaluate. This may have changed in some of the later 5 or newer 6 versions of PHP but it is always safer to use single quotes than double just to be sure.
However as indicated if you use single quotes you must break out of the string and concatenate your variable into the string rather than include it in the string.
On most sites the speed improvement may not be noticable to a user, but on a size with huge load of users it could mean the difference between a successful page and a server crash.
#3

[eluser]slowgary[/eluser]
@tonan:

I've always tried to use single quotes where possible for performance benefits, but recently I was wondering if it really does make a difference. Your post prompted me to test it out. You are right, it does make a difference, but very marginal. I echo'd a long "Lorem Ipsum" string 10000 times, resulting in a page weighing in at a whopping 20mb. Double quote execution time: .9 seconds. Single Quotes: .7 seconds. Marginal but different. Just thought you might like to know.
#4

[eluser]tonanbarbarian[/eluser]
off topic

I have tested this previously as well but could not remember the figures I got, and it was a couple of years ago.
I think I have another post on this forum from a couple of years ago that recommends several things to do to improve performance including
1. No closing ?&gt; on a page if not needed
2. reduce size of loaded files
3. unfortunately function calls slow down processing
i.e. <code>if (is_null($var)) die;</code>
is slower than
<code>if ($var===null) die;</code>
4. use full error reporting when developing because each error, notice, warning etc takes time to process. the live server may be not displaying notices and warnings, or it might even not show errors at all, but each error generated still takes time. So a page full of notices is going to slow down processing
#5

[eluser]slowgary[/eluser]
Wow. Those are GREAT tips, and not necessary intuitive. How does "no closing ?&gt;" make a difference? Also, #2 do you mean "many small PHP files is better than 1 big PHP file"? Thanks.
#6

[eluser]tonanbarbarian[/eluser]
1. no closing ?&gt;
Every time you have a &lt;?php and ?&gt; tag PHP has to switch between PHP code and HTML processing.
So if you have a file which is mostly code, i.e. it starts with &lt;?php and the last line of code in the file is php code, then if you add the ?&gt; at the end of the PHP will switch to HMTL processing.
Now if you have your main script file that includes a second file, if that second file ends with ?&gt;, then PHP will switch to HTML processing after it has passed that file, then when it comes back to the original script file it will have to switch back to PHP processing again.
This all takes time.
However I believe this issue has been resolved or at least lessened somewhat in the newer versions of PHP i.e. 5+, but I have not tested this,
Still good habbit not to end with ?&gt; if not needed.

2. many smaller PHP files is only better than 1 big file if it reduces overall the amount of code loaded
for example if you have a library file that you include that has 1000 lines of code, but each time the library is called only 200 lines on average is used by any given page, it would be better to break that into 2 or more libraries so they can be loaded just as they are needed and there is not any excess code that is not needed being passed.
It is not always possible to do this, but if it is possible it can improve the performance of your code.

For a practical example
If you have a controller that has 4 methods in it, each 200 lines+ of code to process
By general practice CI only really runs 1 method in a controller at a time, so that means 3/4 of the code in the controller is not used for any given request. All of the code that is in the controller must be passed however and that uses memory and CPU cycles and therefore time.
If you put each of the 4 methods into seperate libraries and then modified the original methods to load the corresponding library and execute it the size of the controller would come down dramitically from 800+ lines of code to maybe 20.
Then the library would be loaded with its 200 lines of code.
So 220 lines of code would be loaded rather than 800

You can also do similar to this in the loading of any CI resources. Resist the tempation to autoload anything (unless you have something which is in every page such as the url_helper, authentication libraries etc). Because if you have a method in a controller that simply redirects the user to another page and you are loading helpers and other resources such as models etc that are not used, you are again wasting resources.
While the autoload feature is good it is better to load things as you need them, you know what you need as you are coding so load it before you need it and you will use less resources.




Theme © iAndrew 2016 - Forum software by © MyBB