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

Su: Javascript can’t access directories; php

16-17 January 2022

Starting Point

Status: After learning a bit of Javascript, which does indeed seem pretty straightforward, this morning I learned that it will not work as a way of pulling random images from a directory. I need a server-side language, such as php or Ajax, to do that. Given that I’ve gotten started with php, I think I will continue with that.

Next Steps

  1. So, right now, I can pull random text lines from a file and display them. That will be a nice way of displaying quotations. I think I will tweak that a bit more, and see if I can make it work in the new web page – I still need to figure out how to make php run from an html file, or maybe just see that having a php suffix on the end of my index file is sufficient, and doesn’t look weird to visitors.
  2. After I do that, I can then do the same thing for images. And, ideally, with the php code I’ve already find, I can figure out how to display text associated with each image as well.
  3. 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 blog
  4. My 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

Views: 6

We: First steps in doing php in html

15 January 2022 (and before)

Yesterday, I worked on getting a simple ‘hello world php program to run on my web page.’

First steps in doing php within html

What I learned

  • a file that has a php suffix.
  • php and html files differ considerably in their syntax; they also use different comment characters
  • in working with php it must be run from the server (in contrast to html which can be executed locally)
  • in working with php, it is very helpful to display the web page source – this allows you to see how the php is generating html on the server.
  • To run on my web site, the php must be within a file that has a php suffix
  • It is a good idea to use the long form of the tag <?php ?>
  • php and html files differ considerably in their syntax; they also use different comment characters
  • in working with php it must be run from the server (in contrast to html which can be executed locally)
  • in working with php, it is very helpful to display the web page source – this allows you to see how the php is generating html on the server. ]

What I don’t know / Want to figure out

  • how to get php working when I want my basic page to be named index .html

using php to randomly load content from a file

Today I worked on trying to get a php script to load random images and other content onto my page. I found a php program called “randy.php” that does this sort of thing; it pulls data from two files data.txt and companion.txt, the latter being a optional file to add data to item.

What I learned today

  • txt files, used in this way, are very sensitive. Even though they contain html, the white space matters (unlike in html files).
  • In referencing a local subdirectory, it appears to be a good idea to begin with a leading slash

What I don’t know/ want to figure out

  • Now I want to think about what I really want to do with this sort of functionality, and then look for the best way to implement it. I currently think I would like to do the following types of things :
    • quotes: put up random quotes taken from a file of indefinite length
    • images with text: put up an image with a (sometimes extended) caption.
    • document with text: perhaps put up a link to a paper with a precis or abstract
  • Ideally in all cases I would have a directory for each thing that is randomly selected from; each item

Next Steps

  • I started looking into JavaScript (on w3schools, see notes) thinking it might be more straightforward, and certainly easier to test.

Views: 7