Support » Fixing WordPress » My plugin is not displaying the dollar sign $

  • Resolved Cornwell

    (@cornwell)


    Since our local pub closed due to Covid-19 we have been running the pub quiz online. The the questions and answers are uploaded to a database. I wrote a plugin to read these from the database and display them (see link below). This has been working for over a year. Except…

    Where the question includes a dollar sign the $ is not appearing nor are the figures following it. Question 6 is
    Francoise Bettencourt-Meyers with a fortune of $83bn is thought to be the richest woman in the world. She is the heiress to which fortune?
    but it just says …a fortune of bn is thought to be…

    The code snippet is

    /* Display the questions, with a space for the answers */
    $sql = "SELECT <code>Number</code>, <code>Question</code> FROM <code>$tableName</code> WHERE <code>Date</code> = $quiz AND <code>Type</code> LIKE 'Q' AND <code>Round</code> = $round ORDER BY <code>Number</code>";
    $result = $wpdb->get_results($sql, ARRAY_A);
    if ($result === false) {
    	die ("<p>Reading the questions of round $round fails, MySQL error message: " . $wpdb->print_error() . "</p>\n");
    } else {
    	echo '<form action="/take-the-quiz/answers/?quiz=' . "$quiz&round=$round&rt=$runningTotal\" method=\"POST\">\n";
    	foreach ($result as $row) {
    		extract($row);
    		echo "<p>$Number. <label for=\"Q$Number\">" . esc_html($Question) . "</label></p>\n";
    		echo "<p>Your answer: <input type=\"text\" name=\"answer[$Number]\" id=\"Q$Number\" /></p>\n";
    	}
    	echo '<input type="submit" value="Check your answers">' . "\n";
    	echo "</form>\n";
    }

    I think the problem must be with how WordPress handles the echo statement. I’ve searched for answers without success. Any advice please?

    PS The answer to the quiz question is L’Oreal

    • This topic was modified 11 months ago by .
    • This topic was modified 11 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • corrinarusso

    (@corrinarusso)

    Is this your prod code that is live on the internet???
    Without commenting on the code, I would ask:
    Why do you have this in place –> extract($row);
    as well as this –> esc_html

    You have html stored in the Question in your db?

    If you use a result->fetch_assoc and print it to screen, does the dollar sign display?
    Have you see this with other special characters in the questions?
    What if you enter the dollar sign in like one of these?
    $
    $
    &dollar;

    Thread Starter Cornwell

    (@cornwell)

    Thanks for the quick reply. Here are the answers to your questions:
    I use extract($row) as an easy way to assign the variables $Number and $Question
    esc_html is a late addition to the code, I should probably remove it
    There is no HTML stored in the Question. I have added
    if (($quiz == 44377) && ($round == 7)) print_r($result);
    to the code just after the } else { and you can see the results in the link. The dollar sign does not display. It should say $43bn but it just says bn.

    The GBP sign £ displays correctly.
    I tried using str_replace(‘$’, ‘&dollar;’, $Question) and that displayed as &dollar; because WordPress has changed it to &dollar;

    corrinarusso

    (@corrinarusso)

    Can we confirm that the $43 is actually being stored as a value in the column?
    (trying to make sure that the $43 is actually being removed during the retreival)

    > I use extract($row) as an easy way to assign the variables $Number and $Question

    No.
    Use an associated array:
    $row->Number
    $row->Question

    or, better mysql_fetch_assoc:
    while ($row = mysql_fetch_assoc($result)) {
    echo $row[“Number”] . $row[“Question”]

    ,etc

    Thread Starter Cornwell

    (@cornwell)

    Solution found, but not an explanation for the behaviour. Changing the key line to
    echo "<p>$Number. <label for=\"Q$Number\">" . str_replace('$', '$', $Question) . "</label></p>\n";
    and it works.

    I have proved it is nothing to do with reading the database by setting up a test plugin, where this is the entire code:

    <?php 
    /* This code is included by the WordPress quiz plugin when the text includes [quiz]dollar[/quiz] */
    /* It outputs a page with three versions of a test sentence including a dollar sign. */
    echo "<p>With dollar sign: Francoise Bettencourt-Meyers with a fortune of $83bn is thought to be the richest woman in the world.</p>\n";
    echo "<p>With HTML code: Francoise Bettencourt-Meyers with a fortune of $83bn is thought to be the richest woman in the world.</p>\n";
    echo "<p>With HTML entity: Francoise Bettencourt-Meyers with a fortune of &dollar;83bn is thought to be the richest woman in the world.</p>\n";
    ?>

    You can see the result at https://lovedurham.co.uk/dollar-test/. As you can see, the first test fails but the following two succeed.

    This still leaves the question of what is happening in the first case.

    • This reply was modified 11 months ago by .
    Thread Starter Cornwell

    (@cornwell)

    Hello moderator:
    My repeated editing is because I am trying to include HTML entities in the above code and these are being rendered as the character not the code. So I wanted to enter this (with spaces between the characters so you can see them)
    & # 3 6 ;
    and I ended up with $

    Thread Starter Cornwell

    (@cornwell)

    Closing as partially resolved, with a work-round.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘My plugin is not displaying the dollar sign $’ is closed to new replies.