Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, August 14, 2012

How to disable auto formatting when pasting code in Eclipse

Mac Location:

Open Finder -> Click Go -> Go to Folder

Enter the following  /Users/"YOUR USERNAME"/Documents/workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/

Open org.eclipse.php.ui.prefs in textedit

Add "smartPaste=true"

Save and close.

Source:  Link

***DOESN'T WORK IN OS X***  I didn't try it in windows.

FOUND IT!!!

Click Eclipse -> Preferences -> PHP - > Editor -> Typing -> Uncheck "When Pasting Adjust Indentation" 

Click Ok

HAPPY PASTING!

Friday, June 22, 2012

How to create an AJAX drop down box

My goal in this was to create a dropdown that then when selecting the 1 out of the 4 of the options, part of the webpage would change based on the selection. Here is the HTML:
<select name="ajax" onChange="getinfo(this.value)">
  <option value="" selected="selected">Please Select A Choice</option>
                <option value="option1">option1</option>                         
                <option value="option2">option2</option>
                <option value="option3">option3</option>
                <option value="option4">option4</option>
</select>
Here is the javascript:
<script type="text/javascript">
function getinfo (str)
{
   if (str=="")
      {
        //getElementById is the id tag name innerHTML replaces everything where that tag is with what is after the "="
        document.getElementById("htmlid").innerHTML="";

        return;
      }
          if (window.XMLHttpRequest)
             {
                xmlhttp=new XMLHttpRequest();
             }
          

         xmlhttp.onreadystatechange=function()
            {
               if (xmlhttp.readyState==4 && xmlhttp.status==200)
                 {
                    //readyState 4 means its in a ready state to do whatever, 200 means that its a legit page not 404
                    //split takes the string that is returned with what ever separates the data is in the ""

                     var data = xmlhttp.responseText.split ("/");

  xmlhttp.open("GET","search.php?q="+str,true);

  xmlhttp.send();

 };
The first thing that is done is it goes and "GETS" the page called search.php and pass it the value of q. If you would selection option 1 then the value of option 1 would get passed as the variable q.
At this point you would do whatever you want like search through company names or employee names. At the end of it to return the value you would do this:
echo $foo1 . "/" . $foo2 . "/" . $foo3 . "/" . $foo4. "/"
When the data is sent back it is one long string. I had 4 squares beside each other so I had to go back and forth to get it to show below each other. This required me to split it up. You will see above that I hard coded "/" in between them. That is my value I'm looking for when I called
var data = xmlhttp.responseText.split ("/");

document.getElementById("htmlid").innerHTML=data[x];

var foo = data[x]+" ext "+data[x];
the X is the position that it is located at in the return string when split up. The one below it shows how you could add 2 values that are returned. An example of that would be firstname + lastname. The htmlid is the value in the html code for example:
<td id='htmlid'></td>
When that is called it replaces whatever is currently being displayed next to htmlid. I was also successful in doing this with replacing whitespace with more drop downs to choose from. If I didn't explain this correctly or called something wrong, please post it in the comments and I'll change it.

How to Dynamically Create Variables and Loop through it

So what I wanted to create was a form and the middle had 10 rows.  I wanted a way to loop through all 10 rows without having to have the same code 10 times.

I found by doing

${'foo' . $x}

creates an array that would them make the variables foo1, foo2, ... foo20.

It also works as a session/post variable -> 
$_SESSION['foo' . $x]
$_POST['foo' . $x]
Here's a snippet of code:

$item = 1;

for ($i=0; $i < $num; $i++)
{
$_SESSION['line' .  $item] = mysql_result($result, $i, "table1.x");
$_SESSION['line' .  $item] = mysql_result($result, $i, "table1.x");
$_SESSION['line' .  $item] = mysql_result($result, $i, "table2.x");
$item++;
}

Now I did run into an issue with the sql insert statement. Here is how I got around it:
$sqlqueryitems = "$id', '${'line' . $item }',
                        '${'line' . $item }', 
                        '${'line' . $item }";

$sql = "INSERT INTO table_items (line1, line2, line3)
      VALUES
      ('$sqlqueryitems')";
if (!mysql_query($sqlitems,$con))
{

die('Error: ' . mysql_error($con));

}

How to Display Web Code in Blogger

So scratch that... found a better solution using syntax highlighter!


Login and on the left hand site select Template
Next, click Edit HTML.  It will give you a warning, just click Proceed.
Next do a Control+F or Apple+F to bring up the search box.
Search for "</head>" (minus the quotes)
Add this above it:


<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/> 

<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script> 

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'></script> 

<script language='javascript'> 

SyntaxHighlighter.config.bloggerMode = true;

SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';

SyntaxHighlighter.all();

</script>

So the site I found was off slightly.  But here's how to do it.  Basically your just using CSS to display it slightly differently.


Login and on the left hand site select Template
Next, click Edit HTML.  It will give you a warning, just click Proceed.
Next do a Control+F or Apple+F to bring up the search box.
Search for "]]></b:skin>" (minus the quotes)
Add this above it:


pre { 
background:#efefef; 
border:1px solid #A6B0BF; 
font-size:12px; 
line-height:100%; 
overflow:auto; 
padding:2px; color:#000000 } 
pre:hover { border:1px solid #efefef; }

code { 
font-size:12px; 
text-align:left; 
margin:0;padding:0; color: #000000;} .
clear { clear:both; overflow:hidden; }

Click Preview to make sure it didn't screw up anything. If it's ok, select "Save Template"
Start a New Blog Post

When ever you want to add code, you have to switch it to HTML view and add the <pre class="brush: xxxx"></pre> tags around the code.  You can find a list of the different classes here.

You can then switch back to Compose view after pasting the code. The code requires you to put HTML code in a Quick Escape converter (ex: here).

Source:  Link Link

How to Connect to a MySQL database in PHP

I typically create a file that includes the db connection settings.  So you don't have to repeat that each time you just add "include "dbinfo.php";

dbinfo.php would include something like this:

<?php

//this is hostname or IP, username, password
$con = mysql_connect("localhost","root","root");

if (!$con)

  {

  die('Could not connect: ' . mysql_error($con));

  }

mysql_select_db("yourDBname") or die(mysql_error());

?>

HTTP Error 500 OR PHP Fatal error: Allowed memory size of xxxxxx bytes exhausted while trying to use MPDF

Error Message: HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

And

Error Message: PHP Fatal error: Allowed memory size of xxxxxx bytes exhausted (tried to allocate xxxx bytes) in C:\inetpub\internal portal\mpdf\mpdf.php

The default PHP install on Windows Server 2008 was set at 16 MB.  I updated it to 32 MB.  Keep trying until your pdf will load.  You don't want to go over board.  You also want to set it on that page so that PHP isn't always using that amount of memory.  You can do that by putting this in your php document.

ini_set('memory_limit', '256M');


Source: Link

Wednesday, June 20, 2012

How to setup Eclipse to develop in PHP

This is for OS X.

I first went to here and downloaded the Eclipse classic version.  I also downloaded the 64-bit one.

Another couple of tidbits...

OS:  10.7.3
Using MAMP -> Other MAMP here

First thing you do is extract it.  I then moved the Eclipse folder to /Applications

I then launched Eclipse.  It told me I didn't have Java.  It will launch Software Update and download the correct version of Java.

It will then ask you about setting up a workspace.  If your using MAMP and want to use the htdocs folder, you will want to set the workspace to /Applications/MAMP/.

Note: In the instance you setup the workspace to some where else, go to "File -> Switchwork Space -> Other" to change it.

Next step is to go to Help -> Install New Software. Select the drop down box next to "Work with:" and select "--All Available Sites --". Wait a few minutes while it populates the fields below. In the filter box type PHP and you'll get 3 choices to install. Click finish and they will install.

If you already have existing files in a folder your going to use, I suggest as a backup to do a zip of all the files in the event something goes wrong.  I was fine, but just to be on the safe side, do it!

Then do File New -> Project, Select php project and type htdocs as your folder and it'll import everything you had previously.

If I find any more useful plugins or something else I"ll update this. If anyone wants to suggest plugs or tweaks please post in the comments below.

Source:  Link Link

Tuesday, October 25, 2011

How to Change location of PHP error log

First some important information:

Running Windows Server 2008
PHP Version 5.3.8
Installed in C:\Program Files (x86)\PHP\v5.3\php.ini


Navigate to the PHP folder and open the PHP.ini file.  Be careful because if you screw up this file, PHP won't work anymore.

Navigate to:
[WebPIChanges]
error_log=C:\Windows\temp\php53_errors.log

Change it to:

[WebPIChanges]
;error_log=C:\Windows\temp\php53_errors.log
error_log="C:\some_location\php_errors.log"

This may not work until you restart the php service.  If you restart the IIS Admin Service, it should restart any other service that need restarted.

UPDATE:  I missed a key thing.  You need to add the following to the permissions of the log folder.

domain\users need:

Create Files/ write data Allow
Create Folders / append data Allow

ShareThis