Jump to content

a little help continuing this script please


Michael_Baxter

Recommended Posts

<html>
	<head>
		<title> DaTaBaSe CoNeCtIoN TeStInG PaGe </title>
	</head>
	<body bgcolor="000000">
	<font color="FF0000">

<?php
include "conection.php";

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{


    echo "ID :{$row['id']}  <br> ".
         "NAME : {$row['name']} <br> ".
         " Score: {$row['score']} <br> ".
         " Win:  {$row['win']} <br>".
         "--------------------------------<br>";
} 


echo "Fetched data successfully\n";
mysql_close($conn);
?>


</font>
</body>
</html>

hi I have built this script so far

as you can see my ASOC is out put here however I am trying to add a checkbox called win to each output so that when it is checked it adds the value of 1 to the row score I thought this would be an easy task but everything I try just to get the checkbox onto each record simply results in the page displaying as a blank white page anyone whish to make me some suggestions please

 

Link to comment
Share on other sites

Couple of things.

 

1. While you are learning, try to learn correct way. Mysql is depreciated so you need to use mysqli! You will need to rewrite you're code at one point so it's easier to do it right now.

2. Where is you're checkbox that you want?

3. You will need to implement form

4. You will need onclick event to do what you want (javascript). There are other solutions you can do it.

5. Build you're code the way you want it, tell us what is the error or problem and we will help you out.

 

Hope this put's you on the right track.

Link to comment
Share on other sites

...but everything I try just to get the checkbox onto each record simply results in the page displaying as a blank white page anyone whish to make me some suggestions please

 

If you post the attempted code, we may be able to identify why you're getting a blank page.

 

Do you know if PHP is set to show all errors and warnings? You can make sure by adding the following code to the top of your script during the development / debugging process:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
Link to comment
Share on other sites

I have managed to stop sit back and take a good look at my paretic codes from above and yes I simply deleted them

maybe I should read and re read then think before I post on these forums but neverless here I am again with a new revised set of codes that actually work and have everything on that I said that I wanted, thanks to budimir for the harsh way of dealing with my insulting codes above as it was your short sharp answer that made me think hard about am I trying to learn or bum my answers all the time

after reading all the above comments I now know I need to look into the javascrip one click system as I had not thought of that part yet that's better than having a single update button,

ok so as I said I have added my new codes and now I have an ASSOCs array from MySQL table into a HTML table and I have added a simple checkbox to the right

but the checkbox is still worthless at this point as I'm not sure where to begin with writing the codes to make the query to add the value of 1 to the score

perhaps I should be looking at the javascript for the one click system next

Link to comment
Share on other sites

<html>
	<head>
		<title> DaTaBaSe CoNeCtIoN TeStInG PaGe </title>
	</head>
	<body bgcolor="000000">
	<font color="FF0000">

<?php
include ('conection.php');



//define $result as $con and run the query $sql 
$result = $conn->query($sql);
//if number of rows in the table is higher 0 draw the table 
if ($result->num_rows > 0) {
	echo 
	"<table border= 5 bordercolor= #0000FF><tr><th><font color=#FF0000>ID</th><th><font color=#FF0000>Name</th></tr>";
	//output the data
      while($row = $result ->fetch_assoc()) {
		  //add the results to populate the table 
		  echo "<tr><td><font color= #FF0000>".$row [id]."</td><td><font color= #FF0000>".$row["name"]."</td><td><font color= #FF0000>".$row["score"]."</td><td><input type= checkbox name=win>".$row["win"]."</td></tr>";
		  }
	echo "</table>";
} else {
	echo "0 results found";
}
 
 $conn->close();
 ?>

</font>
</body>
</html>


HAHA your right nit-picking,

 Your now as everyone is always telling me about my punctuation (or lack of),

I did just notice however I did miss something a lot more important that a few ....,,,,,'s from my last post,

I stated that I had revised and re written my code then failed to re show it here,

THIS TIME I AM NOT ASKING FOR HELP I AM MEERLY SHOWING MY RE WORK WHILE IM READING ABOUT JQUERY.AJAX()

Link to comment
Share on other sites

So where is your query defined?  $sql?

 

You could also read up on html tags that are deprecated.  Such as <font>.  And how to use css to make styling your html much simpler. 

 

A major  improvement in script development would be to learn how to keep you html block of code completely separate from the php block of code.  You begin your script with the start of an html doc.  What's the rush?  You should begin with the php code and the logic to determine what exactly you are supposed to do at this instant.  Then do it.  If it involves building some formatted (ie, html) output, then do that as part of your query results loop and store it in a php var that you then embed in your html block where it needs to go.

 

Splitting things up like that makes it Sooooo much easier to follow - for you and for us - and easier to debug later on and easier to enhance and add features such as more html or more php logic.

Link to comment
Share on other sites

OK, you are creating checkboxes, but there is no form. Your initial post kind of alluded to the fact that you may want the update to occur dynamically when the user clicks the checkbox. That is what budimir was referencing when he mentioned JavaScript. That is certainly achievable, but right now let's focus on doing this the old fashioned way - with a form submission. Once you get that working you can re-purpose the logic and make the action dynamic (i.e. when the suer clicks the checkbox a back end call is made via AJAX to update the database without a traditional form submission).

 

Also, since there will be checkboxes for each record - each checkbox needs to be associated with the record that it should be updating

 

The example code below is not optimal. Ideally you should use the PRG pattern to prevent a refresh from resubmitting the form, but in this instance it shouldn't matter. The code below assumes that the 'win' column contains a 0 or 1.

<?php
include ('conection.php');
  
//define $result as $con and run the query $sql 
$result = $conn->query($sql);
 
//Check if the form was posted
if($_SERVER['REQUEST_METHOD']=='POST')
{
    //Update database with posted values
    //Note only CHECKED checkboxes are sent in the POST data
    //Get array of IDs sent in the post data
    $winIDsPostAry = $_POST['win'];
    //Filter out any IDs that are non-numeric or 0
    $winIDsAry = array_filter(array_walk($winIDsPostAry, 'intval'));
    //Create a comma separated string for the query
    $winIDsStr = implode(', ', $winIDsAry);
    //Create query to update all the records based on the checked values
    $query = "UPDATE table_name
              SET win = id IN ({$winIDsStr})";
              
    //Run the query
}
 
 
//Variable to hold the output
$output = '';
 
//Check if there were records returned
if ($result->num_rows == 0)
{
    $output .= "<tr><td colspan='4'>0 results found</td></tr>\n";
}
else
{
    //output the data
    while($row = $result ->fetch_assoc())
    {
        //Determine current state of win
        $winChecked = ($row['win']) ? " checked='checked'": '';
        //add the results to populate the table 
        $output .="<tr>\n";
        $output .="<td>{$row['id']}</td>\n";
        $output .="<td>{$row['name']}</td>\n";
        $output .="<td>{$row['score']}</td>\n";
        $output .="<td><input type='checkbox' name='win[]' value='{$row['id']}' {$winChecked}></td>\n";
        $output .="</tr>";
    }
}
 
$conn->close();
 
?>
 
<html>
    <head>
        <title> DaTaBaSe CoNeCtIoN TeStInG PaGe </title>
        <style>
            body { color: #FF0000; background-color: #000000; }
            .myTable { border-collapse: collapse;}
            .myTable td, th { color: #FF0000; border: 5px solid #0000FF;}
        </style>
    </head>
    <body>
 
    <form action='' method='post'>
    <table class="myTable">
        <tr><th>ID</th><th>Name</th><th>Score</th><th>Win</th></tr>
        <?php echo $output; ?>
    </table>
    </form>
 
 
</body>
</html>

EDIT: I did not test this, so I'm sure there are some errors, but hopefully this will get you pointed in the right direction.

Edited by Psycho
Link to comment
Share on other sites

wow oh wowwy i'm not sure there is an expression to cover that when I just looked over that,

so just to make sure I am understanding everything clearly,

the code posted above by Psycho a re write of my code correctly formatted,

but essentially the end result will still work the same,

you have separated the PHP from HTML,

and generally changed the coding into up to date language.

but now I have changed my code for your code everything seems to work the same as an end result so I still face the problem of finding the .checked function

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.