MAMP

Wednesday 23 February 2022

A couple of weeks ago I installed MAMP. MAMP is a solution stack for web development and stands for Macintosh (OS), Apache , MySQL, PHP. It enables one to run a server based environment on a local machine, and simplifies development because new versions of a site that use php do not have to be uploaded to an external server.

I am now going back to MAMP for php development — that will help avoid the error I had yesterday where I uploaded my php file to the wrong directory and was unable to see any effect of changes I make. With MAMP, I can just edit the file directly.

Figuring out the code

The first thing I will do is figure out the code that makes the random quotations work. Here is the snippet:

<?php

$textfile = 'quotations.txt'; # data file of text lines to display randomly (html ok)

#### READ STRINGS FROM FILE (in same directory) INTO AN ARRAY AND RANDOMIZE THEM
ini_set('auto_detect_line_endings', TRUE);      #alters php settings to detect newlines
$disp = file($textfile, FILE_IGNORE_NEW_LINES); #reads a file into an array
$count = (count($disp)-1);                      #number of lines in array minus 1
$rand = array();                                # declare that rand is an array
for ($a=0;$a<=$count;$a++)                         
    {array_push($rand,$a);}                     # Puts sequence of $count indices into $rand
shuffle($rand);                                 # shuffles indices in $rand

#####Create html formatting and embed choosen string in it
echo '<table id="foo">';
$col_ent = array();                             # declare that $col_ent is an array

$i = 0;
 $key = $rand[$i];                               #put the first element of $rand into $key
 array_push($col_ent,'1');                       #put 1 into #col_ent
echo "<tr><td class='foo-$i'>";

echo "<div class=\"quotationDiv\">";
/* style=\"margin: 10% 15%; text-align: center; font-size:18pt; font-weight:bold;\">"; */
echo "$disp[$key]";  /* the string*/            #display a random string using $key
echo '</div></td></tr></table>';

#Once there was more code that created multiple random entries indexed with col_ent

/*
echo $post;
*/

?>


Ok, I’ve commented the above, and can see that the original code that I copied from this had a lot of leftovers from when it tried to be a far more general function. I think I should be able to rewrite the above so I can just generate a random number and use it as an index to select a random line.

Like this

  • #load the file into the array
  • #determine the length of the array
  • #generate a random index from the set 0 to length-1
    (assuming that php arrays start with 0)
  • #print out the line, embedded in the proper html

That works. The reduced code looks like this:

<?php

$textfile = 'quotations.txt'; # data file of text lines to display randomly (html ok)

#### READ STRINGS FROM FILE (in same directory) INTO AN ARRAY AND RANDOMIZE THEM
ini_set('auto_detect_line_endings', TRUE);      #alters php settings
$quotes = file($textfile, FILE_IGNORE_NEW_LINES);#reads a file into an array
$count = (count($quotes)-1);                     #count number of lines and sub 1
$key=rand(0,$count);                             #generate a random index 

#####Create html formatting and embed choosen string in it
echo '<table id="foo">';

$i = 0;
echo "<tr><td class='foo-$i'>";

echo "<div class=\"quotationDiv\">";
/* style=\"margin: 10% 15%; text-align: center; font-size:18pt; font-weight:bold;\">"; */
echo "$quotes[$key]";  /* the string*/            #display a random string using $key
echo '</div></td></tr></table>';

#Once there was more code that created multiple random entries indexed with col_ent

?>
  • Now I need to remove the legacy formatting… the<td class=’foo-$i’>.
  • And I also need to be crisper on the IDs and class stuff…
  • and the div and table stuff
  • OK, legacy formatting is gone — the table formatting has been discarded and there is just Div.
  • One odd thing that is happening is when I make tweaks to the .php file, they don’t always seem to have an effect right away. It’s as though the original file is cached, though doing what I think is required to force a reload doesn’t make a difference
  • ==> The above problem appears to be due to having ‘lost’ a semi colon in the style section.

The other mystery is that I tried putting all the style code into the main css file and it broke stuff — could that be due to the missing semi-colon???

=> YES
==> ALSO, NEED TO BE CAREFUL TO FORCE A REFRESH OF THE UPLOADED FILES

Views: 4