Write to a file from PHP

I probably covered this before, but I want to keep some log files of what is happening on various pages on a site. I could write to the php error log, but I look at those every morning and then erase them. I want something that sticks around but I don’t need a whole lot.

This solution works for me.


 $fname = "./interesting_stuff.log";
 $info = "Message";
 
 $fp = fopen($fname, 'w');
 fwrite ($fp, "$info" . "\r");
 fclose($fp);

You’ll need to create the file and set the permissions so everyone can write to it before it will work.

Getting one row from a MySQL query

Often you just need one value or one row of values from a table. If your query returns a unique row because of the query or because you limited it to one row, then you don’t need to go through the foreach loop to get the values. You can change the fetchAll() to fetch() and use just one line to get the value.


// get the current category name
$qry  = "SELECT name FROM `website`.`book_category` ";
$qry .= "WHERE id = :categoryID";

$stmt = $dbWG->prepare($qry);
$stmt->bindParam(':categoryID', $categoryID);
$stmt->execute();

$results = $stmt->fetch();
$categoryName = $results['name'];

Breaking URLs and Long Text

When porting a website so that it displays well on phones, I ran across a problem wrapping long text without breaks. This text doesn’t wrap so the whole page gets a horizontal scroll.


<p class='break-word'>On my Windows 7 and 8 computer, with a login name of llsc the location that shows up in the top bar is:
Users\llsc\AppData\Roaming\LocuTourMultimedia\Client Manager\Data
</p>

I added a break-word class to my CSS and it works fine.


.break-word {
    word-wrap:break-word;
    overflow-wrap: break-word;
    word-break: break-word;
}

Responsive iTunes App Ad

When redesigning a site, I was having trouble getting one section to fit on an iPhone 5 without horizontal scrolling. It turns out that there were two issues, one with the Facebook widget and—rather ironically—one with the iTunes App ad.

The ad is in an iFrame and the dimensions are hard coded. e.g.


echo "<iframe src='https://banners.itunes.apple.com/banner.html?partnerId=0419Dm&amp;aId=&amp;id={$app_ID}&amp;c=us&amp;l=en-US&amp;bt=catalog&amp;t=catalog_white&amp;w=300&amp;h=250' style='overflow-x:hidden;overflow-y:hidden;width:300px;height:250px;'></iframe>";

Changing the dimensions either results in a cropped ad or nothing at all. I did however, stumble upon a solution. I reset the iFrame to 80% of the view window.


iframe {
  max-width: 80vw;
  max-height: 80vw;
}