We: Getting Random Quotes to work; errors

19-20 January 2022

Starting Point

I decided I would focus on getting quotes to work as I want them

I did two things first

  • – I mucked about, getting the formatting of the quotes the way I want it
  • And I put the formatting code in the php file, so that the file of quotes can just be text.

That worked well enough

Difficulties, Confusion, and Saving Bits that work

Then I decided I would try to unembed the formatting from the php file. That didn’t work out so well:

  • – I tried having the php just print out formatting that would rely on a css style either pulled from the .css file, or put in the head area in the style definition. Neither of those worked, but I’m not sure why. When I tried running this, the browser complained that it couldn’t load the .css file… perhaps because the browser won’t process a .php file that way? I’m not sure, but at this point I’m getting confused by my experiments, so I’m going to fall back to the last thing that worked, and record that.

And that was having the .php print out the formatting I want just before printing out the item using the randy script. The relevant bit of code looks like this:

echo "<td class='randy-$i'>";
    
   echo "<div style=\"margin: 10% 15%; text-align: center; font-size:24pt; 
         font-weight:bold; background-color:lightblue;\">";
    echo $infix.$disp[$key].$infix;
      if ($companion != '')
      {
      echo '</td>';
      echo "<td class='randy-companion-$i'>";
      echo $infix.$codata[$key].$infix;
      }
    $i++;
    echo '</td>';

So, this gets the code out of text file, and into a single line.

I would like to get it either into an external style sheet, or between style tags in the header.

I would like to get it either into an external style sheet, or between style tags in the header.
•	It may just be a matter of using an index.php file – that appears to access the style sheet, unlike putting the reference to the style sheet in something else. In that case, I do need to figure out how to do internal references to what used to be index.html. I wonder if there is a way to just reference the root file. 
•	I wrote the following line -- echo "<div class=\"quoteDiv\">"; -- but now realize that it probably will cause problems inside at <td> </td>:
•	And I wrote this for a stylesheet:
.BigQuote {
  background-color: lightGray;
  color: black;
  margin: 10% 25%;
  font-size:50px;
} 
And this for an in-html style:
<style>
.bigQuote {
margin: 10% 15%; 
text-align: center; 
font-size:24pt; 
font-weight:bold; 
background-color:lightblue;
}

The class=”active” line in the index.html:   <li><a class="active" href="index.html"><font size="+2">
references the following in the .css file:
.active {
  background-color: #82A3FF; 
  /*  #add8e6 -- aka cornflowerblue */
}
but I’m not sure if this will work because “active” is some kind of special state. 

I’m not sure how to reference the settings that are all part of a style tag. Maybe p.bigQuote? Can I put these things – properites or attributes or whatever inside a <p> element? 

Currently

  • randy1.php works and does latest version of formatting, by putting all formatting in a single line of the php file
  • randy2.php does not work
  • randy3.php works, and references div class=”quoteDiv”, but I can’t find where quoteDiv is defined

Success and Learnings

It’s easy to break things in php

  • It is very easy to ‘break’ a php file by messing up the formatting of an echo statement – leave a ; off and everything breaks with no error messages
  • For echo: Stuff in single quotes is not evaluated; stuff in double quotes is. Backslash can quote a character
  • echo $infix.$disp[$key].$infix; — I don’t understand what is going on here… are the dots connecting? Yes, it is a shorthand for echoing each variable in a different statement.      

randymin1 works

Ok, now I’ve made it to randy1min.php, which does the very simple thing of reading lines from a file into an array, randomizing the array, and displaying the first line. It would be more minimalist if I could determine the number of lines in the file, and read a single random line out of the file – but underneath the hood that might be not be any more efficient. 

#### READ STRINGS FROM FILE INTO AN ARRAY AND RANDOMIZE THEM
ini_set('auto_detect_line_endings', TRUE);
$file = $dpath.$textfile;
$disp = file($textfile, FILE_IGNORE_NEW_LINES);
$count = (count($disp)-1);
$rand = array();
for ($a=0;$a<=$count;$a++) {array_push($rand,$a);} shuffle($rand); # Creates an array of keys then shuffles them

#####Create html formatting and embed choosen string in it
echo '<table id="randy">';
$col_ent = array();
$i = 0;
 $key = $rand[$i];
 array_push($col_ent,'1');
echo "<tr><td class='randy-$i'>";
echo "<div style=\"margin: 10% 15%; text-align: center; font-size:12; font-weight:bold; background-color:lightblue;\">";
echo "$disp[$key]";  /* the string*/
echo '</div></td></tr></table>';

And that works. But I still don’t understand where div.quoteDiv is being defined… I clearly did that at some point, but where is it???? I want to change it!

Next Steps

Get the php-generated html working with style sheets

  • I’d like to figure out what is going on with the <div class=\”quoteDiv\”>”
  • I’d also like to figure out how to make html code generated by a php file use the style sheet – it seems like it should just happen automatically, but I’m not sure that’s the case.

•               

Integration into the site

  • Once I get the random quote display working, I want to
  • Figure out how to make it part of the html site (that probably means using an index.php file as my root, which in turn means I need to figure out how to link to that internally.  I think I have to build index.php links into the menu to get back to the home page…

Random Image Display

  • After quote stuff is integrated and working, I want to do the same thing for image files, mostly likely pulling caption text in to supplement each one.
  • Then, I’m thinking I will
    • find a way to have links to my experimental blogs on my sight, but keep them mostly hidden
    • decide if I feel like I have enough material to start a public blogMy hope is that having a place to ‘publicly’ display stuff I care about will motivate me to document it, and wrap it up in nice packages that will be useful for me later.

Notes

  • randy1.php and randy3.php both work; they differ only in that 3 uses <div class=\”quoteDiv\”>”  rather than inline html.
  • randy1min.php and randy3min.php are stripped down versions of the above; they have all of the general stuff I don’t need stripped out of them; they differ only as their parent files do.

Views: 18