0

i am trying to create a progress bar that will show how far towards a target a user is, which is done by the amount of points they have collected. I have come up with this basic PHP to try and get this to work, however when loading the page, nothing appears, is this due to the lack of styling on the bars? Or there something else within this? Essentially i would like a progress bar to say 25/30 for example and have the bar showing how far along they are? I have tried to get the values to come from the database with the $row function. (I am extremely new to php, so sorry for what probably seems like an easy question)

EDIT: I have now got the bar to appear, but there is nothing inside, it seems as the values are not being placed into the bar, does this require me to create something to change them into a %?

<!DOCTYPE html>
<html lang="en">
    <head></head>
    <body>
    <?php
        $uname ="";
        $password ="";
        $host = "";
        $db = $uname; 
        $connection = mysqli_connect($host,$uname,$password,$db);
        if (mysqli_connect_error()) { 
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        } else {

            $query=mysqli_query($connection,"SELECT * FROM targets WHERE user_id='23'");
            $numrows=mysqli_num_rows($query);
            if($numrows!=0)
            {
                while($row=mysqli_fetch_assoc($query))
                {
                    echo "<progress value=";
                    echo $row['self_points'];
                    echo "max=";
                    echo $row['target_points'];
                    echo "></progress>";
                }
            }
        }
    ?>
    </body>
</html>
Mikk3lRo
  • 3,477
  • 17
  • 38
Sam
  • 1
  • 1
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – ChrisGPT was on strike Apr 16 '18 at 16:41
  • 1
    If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5, and completely removed in PHP 7.0 (which is so old it no longer even receives active support). Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Apr 16 '18 at 16:42
  • Possible duplicate of [PHP's white screen of death](https://stackoverflow.com/q/1475297/354577) – ChrisGPT was on strike Apr 16 '18 at 16:42
  • Thanks @Chris i'll have a look into this. – Sam Apr 16 '18 at 17:14
  • Try to isolate your problem. If you substitute `$row['self_points']` and `$row['target_points']` with plain numbers, does it work as expected? Inspect the HTML: Right click and show source... are the numbers printed correctly in the source? Enable display errors: `ini_set('display_errors', 1);` – Mikk3lRo Apr 16 '18 at 18:06
  • @Mikk3lRo Thank you! I've literally just done this as you were typing! Got it to work thank you! – Sam Apr 16 '18 at 18:09
  • Also... since you're new to PHP... Get your indentation habits right from the beginning, that is one of the most basic and important "skills" to learn... there are different opinions about where to place opening and closing braces (`{` and `}`). But you *must* increase the indentation after an opening brace and decrease it by an equal amount after a closing brace (see my edit of your post). Get it wrong and you lose the overview completely. You should use an editor that makes it very easy for you to get this right. What are you using now? – Mikk3lRo Apr 16 '18 at 18:13
  • @Mikk3lRo Currently using notepad++ to do the editing in :) – Sam Apr 16 '18 at 18:48
  • Ok, that's fine for managing indentation (almost anything except plain notepad is actually) - just select a block of text and hit `tab` to increase indentation and `shift`+`tab` to decrease it. Make sure the opening and closing braces **always** stay aligned and everything inside is indented more. I see I mis-wrote before: You obviously decrease indentation *before* the closing brace, not after :p – Mikk3lRo Apr 16 '18 at 18:59

0 Answers0