Archive for the ‘ PHP ’ Category

Assign MySQL Character Set for PHP output

For the old proceedings/programme pages of old events, we were seeing character errors like this:

Thomas L�we

So to fix this I introduced into the configuration.php file the following:

$db=mysql_connect('localhost','username','password') or mysql_die();
mysql_select_db('DBname',$db);
mysql_set_charset('utf8',$db);


PHP.net reference here.

Query another DB from within WordPress

Add this line to change the default DB to another one:

$wpdb = new wpdb('username','password','database','localhost');

EWEA navigation script

This solution is to create a local HTML file from the dynamic www.ewea.org/navigation-file generated by Typo3, that can be rsync’d to other servers so that they may include it in their templates as local files, speeding up the page creation process (and bypassing a huge latency issue we had with the EWEA 2015 programme pages).

#!/bin/sh
curl -L -o /var/www/html/sites/www.ewea.org/navigation http://www.ewea.org/navigation-file
rsync -avze "ssh -p 10022 -i /root/.ssh/main_prv_key" /var/www/html/sites/www.ewea.org/navigation [email protected]:/var/www/html/
rsync -avze "ssh -p 10022 -i /root/.ssh/main_prv_key" /var/www/html/sites/www.ewea.org/navigation [email protected]:/var/www/html/
rsync -avze "ssh -p 10022 -i /root/.ssh/main_prv_key" /var/www/html/sites/www.ewea.org/navigation [email protected]:/var/www/html/

This is scheduled to run daily at midnight via cron

On the receiving server, the following code is then used to display the navigation on the website.

<div id="navigation">
	<?php // INCLUDE EWEA MENU
	include("/var/www/html/navigation");
	?>
</div><!-- end #navigation -->

Displaying content of a WordPress Form


<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
* {font-family: "franklin gothic book"; font-size: small;}
h2 {font-size: 150%;}
table,td,th {border-collapse: collapse; border: 1px dotted #afafff; padding: 3px 5px;}
th {background: #efefff;}
</style>
</head>
<body>


<?php


// STATE YOUR VARIABLES
////////////////////////////////////////////////////////////////////
	$user  = "annual2015";
	$pass  = "tuYPLUY44DxjRCNB";
	$db    = "annual2015";
	$table = "wp_a15_fm_data_22";
	$sql   = "SELECT * FROM $table ORDER BY `timestamp` DESC LIMIT 0, 500";


// ESTABLISH THE CONNECTION
////////////////////////////////////////////////////////////////////
	$cxn=mysqli_connect("localhost","$user","$pass","$db");
	$cxn->set_charset("utf8");
	if (mysqli_connect_errno())
	{
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}
	$result = mysqli_query($cxn,"$sql");


// GET NUMBER OF ATTENDEES
////////////////////////////////////////////////////////////////////
	$rows = mysqli_num_rows($result);
	printf("<h2>Current number of registered attendees: <span style=\"background: #ff0; font-size: 20px;\">%d</span></h2>",$rows);


// CREATE THE TABLE
////////////////////////////////////////////////////////////////////
echo "<table>
	<tr>
	<th>First name</th>
	<th>Last name</th>
	<th>Job title</th>
	<th>Organisation</th>
	<th>Email address</th>
	<!-- <th>Food requirements</th> -->
	<th>Date registered</th>
	</tr>\n";
while($row = mysqli_fetch_array($result))
	{
	echo "	<tr>\n";
	echo "	<td>" . "<b>" . $row['text-56275de811d4e'] . "</b>" . "</td>\n";
	echo "	<td>" . "<b>" . $row['text-56275dea2cd60'] . "</b>" . "</td>\n";
	echo "	<td>" . $row['text-56275dfd39f14'] . "</td>\n";
	echo "	<td>" . $row['text-56275dfb697d7'] . "</td>\n";
	echo "	<td>" . "<a href=\"mailto:" .$row['text-56275df9b2872'] . "\">" .$row['text-56275df9b2872'] . "</a>" . "</td>\n";
//	echo "	<td>" . $row[''] . "</td>\n";
	echo "	<td>" . $row['timestamp'] . "</td>\n";
	echo "	</tr>\n";
	}
echo "</table>";


// CLOSE THE CONNECTION
////////////////////////////////////////////////////////////////////
mysqli_close($cxn);
?>
</body>
</html>


Exhibitor Press Release Uploads

There are three components to the system:

 

A) The exhibitor upload page

The first thing we need to do is build the form using the “WordPress Form Manager” plugin.
We create a form with the following fields: “Company name”, “Document title” and “Select document”. We also add a note field to display additional information about accepted file formats and file size, etc. Pay attention to the upload document section as to restrict the document types.

We also add a “Private Field” at the bottom of the “Form extra” tab. This is what our administrator will use to approve each press release.

Once you have created the form, add it to the exhibitor upload page with the following shortcode format:
[form form-XYZ]

 

B) The public listing page

We include a simple PHP script into the WordPress page to pull data off the database. The code I used can be obtained here.

To summarise, the script will need to be modified in the following places:

  1. Database name
  2. Form table
  3. User (an account with read-only access just to the form table was created for this application)
  4. Password
  5. The blob variables in the loop

 

C) Administration page

The administrator logs into WordPress and navigates to the Submission Data tab of the form being used.
Using the checkboxes and the “Edit selected” function from the dropdown list of actions, the admin can edit…

form-edit

and approve each press release.

form-approve

Downgrading PHP

I installed PHP on a Centos 6.5 i686 server recently and by default in installed PHP 5.5 which broke some apps.
You can easily downgrade PHP with the following command:

# yum downgrade php*

(Repeat the process again if you want to downgrade a version more)

Displaying the contents of a mysql table in a web page using php

Here is a PHP script I made to display the contents of a workshop registered participants table.

<?php
// STATE YOUR VARIABLES
////////////////////////////////////////////////////////////////////
$user = "workshops";
$pass = "FV8sbyJuJ7bdT5Rj";
$db   = "workshops";
$sql  = "SELECT * FROM `wp_workshops_fm_data_15` LIMIT 0, 100";

// ESTABLISH THE CONNECTION
////////////////////////////////////////////////////////////////////
$cxn=mysqli_connect("localhost","$user","$pass","$db");   //Declare the mysqli connection parameters.
$cxn->set_charset("utf8");   // Define the default character set.
if (mysqli_connect_errno())  // Create a check for connection, if nothing then die.
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($cxn,"$sql");

// GET NUMBER OF ATTENDEES
////////////////////////////////////////////////////////////////////
$rows = mysqli_num_rows($result);
printf("<h2>Current number of registered attendees: %d</h2>",$rows);

// CREATE THE TABLE
////////////////////////////////////////////////////////////////////
echo "<table>
        <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Job title</th>
                <th>Organisation</th>
                <th>Country</th>
                <th>Email address</th>
        </tr>\n";

while($row = mysqli_fetch_array($result))
  {
  echo "        <tr>\n";
  echo "                <td>" . "<b>" . $row['text-526647958e563'] . "</b>" . "</td>\n";
  echo "                <td>" . "<b>" . $row['text-5266498080c9d'] . "</b>" . "</td>\n";
  echo "                <td>" . $row['text-52664992cbe3f'] . "</td>\n";
  echo "                <td>" . $row['text-526649a164f02'] . "</td>\n";
  echo "                <td>" . $row['text-526649bb26849'] . "</td>\n";
  echo "                <td>" . "<a href=\"mailto:" .$row['text-526649d2a727a'] . "\">" .$row['text-526649d2a727a'] . "</a>" . "</td>\n";
  echo "        </tr>\n";
  }
echo "</table>";

// CLOSE THE CONNECTION
////////////////////////////////////////////////////////////////////
mysqli_close($cxn);

?>

PHP emailing script

This is a tested script for correctly sending emails via php:

<?php
$to      = '[email protected]';
$subject = 'This is the subject';
$message = 'Hello! this is the bottom text';
$headers = 'Content-type: text/html; charset="UTF-8"' . "\r\n" .
'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers,"[email protected]");
?>

EWEA Navigation include

Here’s how to include the EWEA navigation file in templates for microsites:

<div id="navigation">
<?php
// INCLUDE EWEA MENU
$naviURL = "http://www.ewea.org/navigation-file/";
if ($fh = fopen($naviURL,"r"))  {
   fpassthru($fh);
} else {
   echo "<p>Unable to load menu.</p>\n";
}
?>
</div><!-- end #navigation -->

PHP cURL to save remote RSS feed to local xml file

Here’s a script I found to get a remote RSS feed, for example the Feedburner feed, and save it to a local XML file.
This would be useful for moments when you cannot connect to the RSS feed directly in a plugin or application.
You could then try and load the local file instead. Obviously a cron task would be added to run this script to reload the RSS feed a couple of times a day.

<?php
$ch = curl_init("http://feeds.feedburner.com/EWEABlog?format=xml");
$fp = fopen("feedburner.xml", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

This script would then write the contents of the RSS feed to the local file “feedburner.xml”