The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

[HTML/PHP] All done? Not quite

guarguar Registered User regular
Yeah, I'm still working on this; sort of lost track of the thread when I moved about 3 weeks ago. Regardless, same old problems, looking for solutions.
css.css
body, textarea {
    font-family: Verdana, san-serif;
    font-size: 7pt;
}

img {
    border-width: 0px;
}

div.total {
    height: 12px;
    width: 500px;
    margin: 15px auto 30px auto;
    text-align: center;
    border: 1px solid black;
}

div.games {
    width: 395px;
    margin-left: 306px;
}

div.progress {
    height: 12px;
    width: 215px;
    margin-top: 8px;
    border: 1px solid black;
    float: left;
}

div.inner {
    height: 12px;
    background-color: gold;
    float: left;
}

.gray25 { color: #bfbfbf; }
.gray50 { color: #808080; }
.red { color: #f00; }
header.php
<?php
    function unfinished($table) {
        $result = mysql_result(mysql_query("SELECT count(*) FROM $table 
                                            WHERE status = 'u' OR status = 'b'"), 0);
        return $result;
    }

    function completed($table) {
        $result = mysql_result(mysql_query("SELECT count(*) FROM $table 
                                            WHERE status = 'c'"), 0);
        return $result;
    }

    function total($table) {
        $result = mysql_result(mysql_query("SELECT count(*) FROM $table 
                                            WHERE status != 'w'"), 0);
        return $result;
    }

    function display($table, $ids, $names, $cmnt, $stat) {
        $n = mysql_num_rows($names);
        for($i = 0; $i < $n; $i++) {
            echo "<b>";
            if(mysql_result($stat, $i) == 'w') {
                echo "<span class=\"gray50\">";
                echo      mysql_result($names, $i);
                echo "</span>";
            }
            else
                echo mysql_result($names, $i);

            echo "</b>";
            if(mysql_result($cmnt, $i) != null) {
                echo "<br />";
                echo "<span class=\"gray25\">";
                echo mysql_result($cmnt, $i);
                echo "</span>";
            }

            $id = mysql_result($ids, $i);

            echo "<br />";
            if(mysql_result($stat, $i) != 'c') {
                echo "<a href=\"edit.php?table=$table&id=$id\" onclick=\"return popup(this, 'edit')\">Edit</a>";
                echo " | ";
                echo "<a href=\"delete.php?table=$table&id=$id\">Delete</a>";
            }

            echo "<p />";
        }
    }

    function sortBy($column, $table) {
        $query = mysql_query("SELECT $column FROM $table 
                              GROUP BY TRIM(LEADING 'A ' FROM 
                                       TRIM(LEADING 'An ' FROM 
                                       TRIM(LEADING 'The ' FROM alt_name)))");
        return $query;
    }
?>

<script type="text/javascript">
    function show(id) {
        if(document.getElementById(id).style.display != '')
            document.getElementById(id).style.display = '';
        else
            document.getElementById(id).style.display = 'none';
    }

    function popup(link, id) {
        if(!window.focus) return true;
        window.open(link.href, id, 'width=427, height=370');
        return false;
    }

    function check() {
        if(document.form.title.value == "")
            return false;
        else
            return true;
    }
</script>
index.php
<?php
    require('header.php');

    $link = mysql_connect('localhost', 'root', '******');
    $db = mysql_select_db('backlog');

    $ds_u = unfinished(ds); $ds_c = completed(ds); $ds_tot = total(ds);
	
    $tot_u = $ds_u;
    $tot_c = $ds_c;
    $total = $ds_tot;

    $ds_id = sortBy(id, ds); $ds_names = sortBy(name, ds);
        $ds_cmnt = sortBy(comment, ds); $ds_stat = sortBy(status, ds);

    mysql_close($link);
?>

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Gaming Backlog</title>
        <link href="css/css.css" rel="Stylesheet" type="text/css" />
    </head>
    <body>
        <div class="total">
            <?php
                $total_width = 0;
                if($total != 0)
                    $total_width = ($tot_c/$total) * 500;
            ?>
            <div class="inner" style="width: <?= $total_width ?>px"></div>
        </div>
        <div class="games">
            <div style="float: left">
                <img src="images/ds.png"
                     onmouseover="this.src='images/ds_sel.png'"
                     onmouseout="this.src='images/ds.png'"
                     ondblclick="show('ds')" />
            </div>
            <div style="float: right">
                <div class="progress">
                    <?php
                        $progress_width = 0;
                            if($ds_tot != 0)
                                $progress_width = ($ds_c/$ds_tot) * 215;
                    ?>
                    <div class="inner" style="width: <?= $progress_width ?>px"></div>
                </div>
                <div style="margin: 10px 0px 0px 5px; float: right">
                    <a href="add.php?table=ds" onclick="return popup(this, 'add')">
                        <img src="images/add.png" />
                    </a>
                </div>
            </div>
            <div id="ds" style="display: none; padding: 35px 0px 0px 30px;">
                <?= display(ds, $ds_id, $ds_names, $ds_cmnt, $ds_stat) ?>
            </div>
        </div>
    </body>
</html>
add.php
<?php
    require('header.php');

    if(isset($_POST[submit])) {
        $link = mysql_connect('localhost', 'root', '******');
        $db = mysql_select_db('backlog');

        $table = strval($_GET['table']);

        if($_POST['alt_name'] == "")
                $_POST['alt_name'] = $_POST['title'];

        $query = null;
        $query = sprintf("INSERT INTO $table (name, alt_name, comment, status)
                          VALUES ('%s', '%s', '%s', '$_POST[status]')",
                                  mysql_real_escape_string($_POST['title']),
                                  mysql_real_escape_string($_POST['alt_name']),
                                  mysql_real_escape_string($_POST['comment']));

        if(!is_null($_POST['title']))
            mysql_query($query);

        mysql_close($link);

        echo "<script type=\"text/javascript\">";
        echo    "window.close();";
        echo    "opener.location.reload();";
        echo "</script>";
    }
?>

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <link href="css/css.css" rel="Stylesheet" type="text/css" />
    </head>
    <body>
        <form name="form" method="post" onsubmit="return check()">
            Title: <br />
                <textarea name="title" rows="2" cols="75"></textarea> <p />
            Alt Name: <br />
                <textarea name="alt_name" rows="2" cols="75"></textarea> <p />
            Comment: <br />
                <textarea name="comment" rows="5" cols="75"></textarea> <p />

            <input type="radio" name="status" value='u' checked />Unfinished <br />
            <input type="radio" name="status" value='b' />Beaten <br />
            <input type="radio" name="status" value='c' />Completed <br />
            <input type="radio" name="status" value='w' />Wishlist <p />
            <input type="submit" name="submit" value="Add" />
        </form>
    </body>
</html>
update.php
<?php
    $link = mysql_connect('localhost', 'root', '******');
    $db = mysql_select_db('backlog');

    $table = strval($_GET[table]);
    $id = intval($_GET[id]);

    $sys_name = null; $sys_alt = null; $sys_cmnt = null; $sys_stat = null;
    if(!is_null($id)) {
        $sys_name = mysql_result(mysql_query("SELECT name FROM $table WHERE id = $id"), 0);
        $sys_alt = mysql_result(mysql_query("SELECT alt_name FROM $table WHERE id = $id"), 0);
        $sys_cmnt = mysql_result(mysql_query("SELECT comment FROM $table WHERE id = $id"), 0);
        $sys_stat = mysql_result(mysql_query("SELECT status FROM $table WHERE id = $id"), 0);
    }

    $query = null;
        if(!is_null($_POST[name])) {
            $query = sprintf("UPDATE $table SET name = '%s', alt_name = '%s', comment = '%s',
                                            status = '$_POST[status]' WHERE id = $id",
                          mysql_real_escape_string($_POST[name]),
                          mysql_real_escape_string($_POST[alt_name]),
                          mysql_real_escape_string($_POST[comment]));
    }

    if($query != null)
        mysql_query($query);

    mysql_close($link);

    if(isset($_POST[submit])) {
        echo "<script type=\"text/javascript\">";
        echo    "window.close();";
        echo    "opener.location.reload();";
        echo "</script>";
    }
?>

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <link href="css/css.css" rel="Stylesheet" type="text/css" />
    </head>
    <body>
        <form method="post">
            Name: <br />
                <textarea name="name" rows="2" cols="75"><?= $sys_name ?></textarea> <p />
            Alternative: <br />
                <textarea name="alt_name" rows="2" cols="75"><?= $sys_alt ?></textarea> <p />
            Comment: <br />
                <textarea name="comment" rows="5" cols="75"><?= $sys_cmnt ?></textarea> <p />

            <?php
                $u_check = null; $b_check = null; $c_check = null; $w_check = null;
                if($sys_stat == 'u') $u_check = checked;
                else if($sys_stat == 'b') $b_check = checked;
                else if($sys_stat == 'c') $c_check = checked;
                else if($sys_stat == 'w') $w_check = checked;
            ?>

            <input type="radio" name="status" value='u' <?= $u_check ?> />Unfinished <br />
            <input type="radio" name="status" value='b' <?= $b_check ?> />Beaten <br />
            <input type="radio" name="status" value='c' <?= $c_check ?> />Completed <br />
            <input type="radio" name="status" value='w' <?= $w_check ?> />Wishlist <p />
            <input type="submit" name="submit" value="Update" />
        </form>
    </body>
</html>
delete.php
<?php
    header('Location: http://localhost/index.php');

    $link = mysql_connect('localhost', 'root', '******');
    $db = mysql_select_db('backlog');

    $table = strval($_GET['table']);
    $id = intval($_GET['id']);

    mysql_query("DELETE FROM $table WHERE id = $id");

    mysql_close($link);
?>

guar on

Posts

  • guarguar Registered User regular
    edited July 2009
    Seem to have stumbled upon a solution.

    add.php
    <?php
        require('header.php');
    
        $link = mysql_connect('localhost', 'root', '******');
        $db = mysql_select_db('backlog');
    
        $table = strval($_GET['table']);
    
        $query = null;
        $query = sprintf("INSERT INTO $table (name, alt_name, comment, status)
    		      VALUES ('%s', '%s', '%s', '$_POST[status]')",
    			      mysql_real_escape_string($_POST[title]),
    			      mysql_real_escape_string($_POST[alt_name]),
    			      mysql_real_escape_string($_POST[comment]));
    
        if([COLOR="Red"][B]$_POST[title][/B][/COLOR] != null)
    	mysql_query($query);
    
        mysql_close($link);
    ?>
    

    guar on
  • InfidelInfidel Heretic Registered User regular
    edited July 2009
    First, test a variable with the is_null function, not comparing to null.

    Second, you are trying to "process the form" even when you first load the page, hence your blank entries.

    Wrap your php code at the top of your page in an IF block that tests for a set variable, the standard way to handle a form is to give the submit control on your form a name and make sure that is set before processing.
    ...
    
    [b]if (isset($_POST['submit'])) {[/b]
    
        $link = mysql_connect('localhost', 'root', '******');
    
        ...
    
        mysql_close($link);
    
    [b]}[/b]
    
    ...
    
    <input type="submit" name="submit" value="Add" />
    

    Infidel on
    OrokosPA.png
  • guarguar Registered User regular
    edited July 2009
    Great, thanks.

    Next up, trying to close the window after submitting.

    guar on
  • SporkAndrewSporkAndrew Registered User, ClubPA regular
    edited July 2009
    Do you need to close it? Can you not just open it in the same window and then use a header("location: index.php?saved=1") or something to then display a saved message if the transaction was successful?

    SporkAndrew on
    The one about the fucking space hairdresser and the cowboy. He's got a tinfoil pal and a pedal bin
  • guarguar Registered User regular
    edited July 2009
    Would it not be possible, or why would I need to open it in the same window? I know you can assign a variable to the call to window.open, which can be used with window.close, but why doesn't it work here?

    ed: Think I have it.

    add.php
    <?php
        require('header.php');
    
        if(isset($_POST[submit])) {
            $link = mysql_connect('localhost', 'root', '******');
            $db = mysql_select_db('backlog');
    
            $table = strval($_GET['table']);
    
            $query = null;
            $query = sprintf("INSERT INTO $table (name, alt_name, comment, status)
                              VALUES ('&#37;s', '%s', '%s', '$_POST[status]')",
                                      mysql_real_escape_string($_POST[title]),
                                      mysql_real_escape_string($_POST[alt_name]),
                                      mysql_real_escape_string($_POST[comment]));
    
            if(!is_null($_POST[title]))
                mysql_query($query);
    
            mysql_close($link);
    
            [COLOR="Red"][B]echo "<script type=\"text/javascript\">";
            echo    "window.close();";
            echo    "opener.location.reload();";
            echo "</script>";[/B][/COLOR]
        }
    ?>
    [SIZE="6"][B]. . .[/B][/SIZE]
    

    guar on
  • DeathPrawnDeathPrawn Registered User regular
    edited July 2009
    Infidel wrote: »
    First, test a variable with the is_null function, not comparing to null.

    I usually just test for null in php by referencing the variable - i.e. "if(!$variable)". Is there a reason I shouldn't be doing that?

    DeathPrawn on
    Signature not found.
  • InfidelInfidel Heretic Registered User regular
    edited July 2009
    DeathPrawn wrote: »
    Infidel wrote: »
    First, test a variable with the is_null function, not comparing to null.

    I usually just test for null in php by referencing the variable - i.e. "if(!$variable)". Is there a reason I shouldn't be doing that?

    Depends on the logic of your code whether it will be a problem or not.

    Programming practice just dictates playing it safe with null, and testing with the appropriate tools when given to you.

    In PHP null behaves like this, the following are all true:

    0 == null
    "" == null
    null == null

    This is not consistent with most languages, null-handling is often different from language to program to whatever, but if they offer an is_null test then it's probably best to use it.

    You could legitimately test for a null variable in PHP by using the strict type equality, ===.

    $var = 0;
    $var == null (true)
    $var === null (false)

    Infidel on
    OrokosPA.png
  • guarguar Registered User regular
    edited July 2009
    Updated the OP with all my code.

    I'm pretty much done I think; not much else I want to do. Just one more, "would you mind looking over this?" and much kudos will be had.

    guar on
  • guarguar Registered User regular
    edited July 2009
    Had a hiccup.
    frontend.png
    My guess is it has something to do with the <div> layout. I've tried breaklines, empty <div>'s, starting a new <div> for each system, etc. No dice.

    Also, for input, all apostrophes are preceded by a forward slash; do I just need to add an if statement in my code to catch this?

    guar on
  • Mr_RoseMr_Rose 83 Blue Ridge Protects the Holy Registered User regular
    edited July 2009
    Try adding sufficient padding around your progress bars to that the div is the same height as the image.

    Mr_Rose on
    ...because dragons are AWESOME! That's why.
    Nintendo Network ID: AzraelRose
    DropBox invite link - get 500MB extra free.
  • guarguar Registered User regular
    edited July 2009
    I tried that.. but not for both the progress bar and the image. Go figure. Thanks!

    guar on
  • guarguar Registered User regular
    edited July 2009
    guar wrote: »
    Also, for input, all apostrophes are preceded by a forward slash; do I just need to add an if statement in my code to catch this?

    Backslash, forward slash. Found out about the built-in function stripslashes($str), which removes any backslash from view, but it doesn't remove them from the entry. If I were to edit an entry with the escape string already in place, it would add another set, which would then be displayed.

    To illustrate, say I have an entry King's Quest that is King/'s Quest in the database. Editing this entry without first removing the backslash would produce King///'s Quest and so forth.

    Can I prevent this behavior entirely? Is there another built-in method for what I'm looking for, or maybe a trim method of some sort would suffice?

    guar on
  • InfidelInfidel Heretic Registered User regular
    edited July 2009
    Sounds like you might have magic quotes on, which is bad.

    http://ca2.php.net/manual/en/security.magicquotes.disabling.php

    Infidel on
    OrokosPA.png
  • guarguar Registered User regular
    edited July 2009
    I had one of the values set; turned that off, but I'm still getting automatic backlashes.

    ed: If it isn't something I can do, then don't worry about. I can always re-edit an entry and I can live with a couple unseen backslashes.

    guar on
Sign In or Register to comment.