Forum Moderators: coopster
at below code i dont know how to output more then just one variable!
Please help
---------------
$result_array = array();
for ($count = 1; $row = mysql_fetch_row ($resultt); ++$count)
{
$result_array[$count] = addslashes($row[0]);
}
return $result_array;
}
-----------------------
function display_user_results
function display_user_results($result_array)
{
global $result_table;
$result_table = true;
?>
<br />
<form name=result_table action="delete_results.php" method=POST>
<table bgcolor=#bde1f0>
<?php
$color = "#3194ce";
if (is_array($result_array) && count($result_array)>0)
{
foreach ($result_array as $ex1, $ex2, $ex3, $ex4, $ex5, $ex6, $ex7, $ex8, $ex9, $ex10, $ex11, $ex12, $ex13, $ex14, $ex15, $ex16, $ex17, $ex18, $ex19, $ex20, $ex21, $ex22, $ex23, $ex24, $sum)
{
echo "<tr bgcolor=$color><td><a href=\"$ex1\">".htmlspecialchars($ex1)."</a></td>";
echo "</tr>";
<removed repetitive code>
echo "<tr bgcolor=$color><td><a href=\"$sum\">".htmlspecialchars($sum)."</a></td>";
echo "</tr>";
}
}
else
echo "<tr><td>Trenutno ni nobenega zapisa.</td></tr>";
?>
</table>
</form>
<?php
}
lp Alenkolina
[edited by: jatar_k at 5:24 pm (utc) on Jan. 19, 2004]
[edit reason] fixed sidescroll [/edit]
I just have to output them here on the screen. I allredy have them in the database.
foreach ($result_array as $ex1, $ex2, $ex3, $ex4, $ex5, $ex6, $ex7, $ex8, $ex9, $ex10, $ex11, $ex12, $ex13, $ex14, $ex15, $ex16, $ex17, $ex18, $ex19, $ex20, $ex21, $ex22, $ex23, $ex24, $sum)
Maybe i did not declare my problem the write way, sorry for that but my english is not so very good.
SO: in a database there is a tabel like:
userEx1 Ex2...Ex24sum
gh 2 3 05
fg0 0 11
...
And that tabel should appear on the users screen.
Have i been at least a little been clearer.
Alenka
list($user, $ex1, $ex2, $ex3, $ex4, $ex5, $ex6, $ex7, $ex8, $ex9, $ex10, $ex11, $ex12, $ex13, $ex14, $ex15, $ex16, $ex17, $ex18, $ex19, $ex20, $ex21, $ex22, $ex23, $ex24, $sum) = $result_array;
Another way is to replace the entire foreach loop you have (including the html code) and replace it with:
$numelements = count($result_array);
for ($i=1;$i<$numelements;$i++) {
echo "<tr bgcolor=$color><td><a href=\"{$result_array[$i]}\">".htmlspecialchars({$result_array[$i]})."</a></td>";
echo "</tr>";
}
Hope this is of some use.
Then i got another solution and this one works very well but there is one mistake--Every time user clicks refresh button the values are stored in the database again. So if user clicks refresh button the output doubles.
My code now is:
$x1a = 1;
$x1aa = 2;
$x1b = 1;
$x1bb = 3;
$x2a = 0;
$x2aa = 0;
$x2b = 0;
$x2bb = 0;
$x2c = 0;
$x2cc = 4;
$x3a = 6;
$x3b = 5;
$x3c = 6;
$x4a = 6;
$x4b = 6;
$x5a = 4;
$x5b = 6;
$x5c = 6;
$x6pl = 6;
$x6ob = 6;
$x7a = 5;
$x7b = 6;
$x8a = 0;
$x8b = 0;
$x8c = 0;
$x9 = 0;
$x10 = 0;
$x11a = 0;
$x11b = 0;
$x12 = 0;
$sum = 55;
// poskusi dodati zapis
if (add_result($x1a,$x1aa,$x1b,$x1bb,$x2a,$x2aa,$x2b, $x2bb,$x2c,$x2cc,$x3a,$x3b,$x3c,$x4a,$x4b,$x5a,$x5 b,$x5c,$x6pl,$x6ob,$x7a,$x7b,$x8a,$x8b,$x8c,$x9,$x10,$x11a,$x11b,$x12,$sum))
echo '<p> </p>Tvoji rezultati so bili shranjeni v bazo.<p> </p>';
else
echo '<p> </p>Zapisa ni bilo mogoče dodati.<p> </p>';
global $HTTP_SESSION_VARS;
$valid_user = $HTTP_SESSION_VARS['valid_user'];
$connection=db_connect();
$query = "SELECT * FROM results WHERE username='$valid_user'";
$result = mysql_query($query , $connection) or die("<b>Query Failed</b><br>\n".$query."<br>\n".mysql_errno().": ".mysql_error());
// poberi imena polj iz baze
$total_number_of_fields = mysql_num_fields($result);
for ($i = 0; $i < $total_number_of_fields; $i++) {
$keys[$i] = mysql_field_name($result , $i);
}
$color="#3194ce";
// začni tabelo
echo "<center><table bgcolor=#bde1f0>";
// naslovna vrstica
echo "<tr bgcolor=$color>";
foreach ($keys as $key) {
echo "<td><strong>".$key."</strong></td>";
}
echo "</tr>";
// rezultati
while ($row = mysql_fetch_assoc($result)) { // čez vse vrstice
echo "<tr>";
foreach ($keys as $key) { // čez vsako kolono te vrstice
echo "<td>";
echo (is_null($row[$key]) && empty($row[$key]))? " " : $row[$key];
echo "</td>";
}
echo "</tr>";
}
echo "</table></center><p> </p>";
// sprosti rezultate iz spomina
mysql_free_result($result);
unset($keys,$row);
mysql_close($connection);
The connection with database is made before, i di not put the code in here.
LP Alenka
alenka99:
Thanks for the answers. The last solution outputed the tabel, but i couldnt get it to output the users values.
echo "<tr bgcolor=$color><td><a href=\"{$result_array[$i]}\">".htmlspecialchars($result_array[$i])."</a></td>";
echo "</tr>";
}
So i wil totaly understand if you will say go to hell.
But since i dont have anyone to ask i will try anyway.
The code:
1.add_results.php--> here i must add that variables have constant values, since it is easier to work. i will change them then when my script wil work fine
<modnote>Way too much code see charter [webmasterworld.com] re: code dumps</modnote>
Whell that is all the code i have, as i have said i woul extremly appreciate if you could look at the code and help me.
BY, Alenka
[edited by: jatar_k at 5:25 pm (utc) on Jan. 19, 2004]
Appologies in advance. I know english is not your first language, so I will describe my terms.
Horizontal:
¦ Nal 1.a) ¦ Nal 1.aa) ¦ Nal 1.b) ¦ Nal 1.bb) ¦ ... ¦
¦ 1 ¦ 1 ¦ 1 ¦ 1 ¦ ... ¦
.
.Vertical:
¦ Nal 1.a) ¦ 1 ¦
¦ Nal 1.aa) ¦ 1 ¦
¦ Nal 1.b) ¦ 1 ¦
¦ Nal 1.bb) ¦ 1 ¦
.
.
.
I'm assuming that $results_array actually does contains the data you want. Do you get any output at all from your existing code?
For a horizontal output, replace your display_user_results function with the following.
function display_user_results($result_array)
{
//prikaži tabelo zapisov
// postavi globalno spremenljivko, da bomo kasneje lahko testirali kaj je na strani
global $result_table;
$result_table = true;
?>
<br />
<form name=result_table action="delete_results.php" method=POST>
<table bgcolor=#bde1f0>
<?php
$color = "#3194ce";
echo "<tr bgcolor=$color>
<td><strong>Nal 1.a)</strong></td>";
<snipped extra code>
<td><strong>Nal 12.</strong></td>";
<td><strong>Vsota</strong></td>";
echo "<td><strong>Zbriši?</strong></td>";
echo "</tr>";
if (is_array($result_array) && count($result_array)>0) {
$numelements = count($result_array);
echo "<tr bgcolor=$color>";
for ($i=1;$i<$numelements;$i++) {
echo "<td><a href=\"{$result_array[$i]}\">".htmlspecialchars($result_array[$i])."</a></td>";
}
echo "</tr>";
}
else
echo "<tr><td>Trenutno ni nobenega zapisa.</td></tr>";
?>
</table>
</form>
<?php
}
Mostly this changes your header into a single row of <td>'s followed by a single row of the data in $result_array. Changing this into a vertical list is a bit more complicated, but lets see if this code works first.
Also, please describe what you see and what you think needs to be fixed. This will make debugging easier.
[edited by: jatar_k at 5:17 pm (utc) on Jan. 19, 2004]
[edit reason] see note in msg 10 [/edit]
See now i got the another solution for displaying the records but here i have another problem, cos i dont know how to add a colon for deleting the records.
The below script is what i have now:
if (add_result($x1a,$x1aa,$x1b,$x1bb,$x2a,$x2aa, $x2b,$x2bb,$x2c,$x2cc,$x3a,$x3b,$x3c,$x4a,$x4b, $x5a,$x5b,$x5c,$x6pl,$x6ob,$x7a,$x7b,$x8a,$x8b, $x8c,$x9,$x10,$x11a,$x11b,$x12,$sum))
echo '<p> </p><h3>Tvoji rezultati so bili shranjeni v bazo.</h3>Za ogled rezultatov bazi klikni na spodnji link Moji rezultati.<p> </p>';
else
echo '<p> </p>Zapisa ni bilo mogoče dodati.<p> </p>';
global $HTTP_SESSION_VARS;
$valid_user = $HTTP_SESSION_VARS['valid_user'];
$connection=db_connect();
$query = "SELECT * FROM results WHERE username='$valid_user'";
$result = mysql_query($query , $connection) or die("<b>Query Failed</b><br>\n".$query."<br>\n".mysql_errno().": ".mysql_error());
// poberi imena polj iz baze
$total_number_of_fields = mysql_num_fields($result);
for ($i = 0; $i < $total_number_of_fields; $i++) {
$keys[$i] = mysql_field_name($result , $i);
}
$color="#3194ce";
// začni tabelo
echo "<center><table bgcolor=#bde1f0>";
// naslovna vrstica
echo "<tr bgcolor=$color>";
foreach ($keys as $key) {
echo "<td><strong>".$key."</strong></td>";
}
echo "</tr>";
// rezultati
while ($row = mysql_fetch_assoc($result)) { // čez vse vrstice
echo "<tr>";
foreach ($keys as $key) { // čez vsako kolono te vrstice
echo "<td>";
echo (is_null($row[$key]) && empty($row[$key]))? " " : $row[$key];
echo "</td>";
}
echo "</tr>";
}
echo "</table></center><p> </p>";
// sprosti rezultate iz spomina
mysql_free_result($result);
unset($keys,$row);
mysql_close($connection);
[edited by: jatar_k at 5:21 pm (utc) on Jan. 19, 2004]
Let as say that curent user has saved 5 records and he has decided that he wants to delete the first and the third record,m the whole row.
I would like to have at the end of each row a button for deliting a record or an option to check if a user wants to delete marked record.
I hope i explained a little better.
lp Alenka
Lets do this in steps.
The first issue is to uniquely identify every record in the database. The simplest way to do that is to add a primary key field named 'ID' and set it to NOT NULL and auto_increment. This should be simple to do if you are using phpMyAdmin to setup your table.
Next you need to add a single column to your output table with something like this as it's content:
echo "<td><a href=\"del_record.php?id={$row['ID']}&return_to={$_SERVER['PHP_SELF']}\">Delete</a></td>";
Now create another php file named 'del_record.php' in the same directory as your current code. In it you will need something like:
<?php
include ('<your db_connect() code>');
$connection=db_connect();
mysql_query("DELETE FROM results WHERE `ID`={$_GET['id']} LIMIT 1");
header ("Location: {$_GET['return_to']}");
?>
If everything works right, clicking on the new 'Delete' link will load the new del_record.php which will delete the row, then refresh the current page. You will need to make sure you properly handle this refresh. In other words, do not add the record back into the table.
There is little error checking in this code, and there are some security issues that should be looked at.
For tonight i would have just one more question-->is it posible to prevent that user could click the refresh button on IE or that the page would ignore the clicked refresh button-->see i have problems with doubeling the records saved in the database.
lp Alenka
$x1a = 1;
$x1aa = 2;
.
.
.
$x12 = 0;
$sum = 55;// poskusi dodati zapis
if (add_result($x1a,$x1aa,$x1b,$x1bb,$x2a,$x2aa,$x2b,
$x2bb,$x2c,$x2cc,$x3a,$x3b,$x3c,$x4a,$x4b,$x5a,$x5b,
$x5c,$x6pl,$x6ob,$x7a,$x7b,$x8a,$x8b,$x8c,$x9,$x10,$x11a,$x11b,$x12,$sum))
echo '<p> </p>Tvoji rezultati so bili shranjeni v bazo.<p> </p>';
else
echo '<p> </p>Zapisa ni bilo mogoče dodati.<p> </p>';
$x1a = 1;
$x1aa = 2;
.
.
.
$x12 = 0;
$sum = 55;if(isset($_POST['submit'])) {
// poskusi dodati zapis
if (add_result($x1a,$x1aa,$x1b,$x1bb,$x2a,$x2aa,$x2b,
$x2bb,$x2c,$x2cc,$x3a,$x3b,$x3c,$x4a,$x4b,$x5a,$x5b,
$x5c,$x6pl,$x6ob,$x7a,$x7b,$x8a,$x8b,$x8c,$x9,$x10,$x11a,$x11b,$x12,$sum))
echo '<p> </p>Tvoji rezultati so bili shranjeni v bazo.<p> </p>';
else
echo '<p> </p>Zapisa ni bilo mogoče dodati.<p> </p>';
}
echo "<form name=\"input_data\" action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">
echo "<input type=\"submit\" value=\"Submit\"></input>
echo "</form>
Later you can replace all the assigns like '$x1a = 1;' with '$x1a = $_POST['x1a']' and add and '<input type="text" name="x1a"></input>' to the form.
Right now this will place a submit button that will only update the database when it is pressed. Later, by adding all the 'form' elements instead of the assigns, it will allow the user to input the data into the database.
Again, this is a simplified version. You will need to add error checking.
Now i have made it so, that function for adding the recors is separated from the validatin. But whatever i do i can not prevent that user could refresh the page where the function add_record is called and in that case the output doubles.
I hope you can see where is the problem.
l Alenka
if (add_result($x1a,$x1aa,$x1b,$x1bb,$x2a,$x2aa,$x2b, $x2bb,$x2c,$x2cc,$x3a,$x3b,$x3c,$x4a,$x4b,$x5a, $x5b,$x5c,$x6pl,$x6ob,$x7a,$x7b,$x8a,$x8b,$x8c, $x9,$x10,$x11a,$x11b,$x12,$sum))
{
echo '<p> </p><h3>Tvoji rezultati so bili shranjeni v bazo.<p></h3>Za ogled rezultatov v bazi klikni na spodnji link Moji rezultati ali pa na povezavo:</p>';
do_html_URL('display_results.php','<center>Želim si ogledati rezultate shranjene v podatkovni bazi!</center>');
echo '<p> </p>';
}
else
echo '<p> </p>Zapisa ni bilo mogoče dodati.<p> </p>';
2. the ID number is shown on the screen but i would like just to be writen Delete without the Id number.
Thanks a lot, i just hope you can help me a little more with that thing to work properly.
BY Alenka
[edited by: jatar_k at 4:29 pm (utc) on Jan. 20, 2004]
[edit reason] sidescroll [/edit]
One thing that I have found that helps in debugging SQL statements is to echo the query to the screen. Then copy that query to the SQL window of phpMyAdmin. If there is a syntax error in the SQL statement, then phpMyAdmin will point you to where the error is.
Did you mean to do the following:
echo "mysql_query( "insert into results values
('$valid_user', '$x1a', '$x1aa', '$x1b', '$x1bb', '$x2a', '$x2aa', '$x2b', '$x2bb', '$x2c', '$x2cc', '$x3a', '$x3b', '$x3c', '$x4a', '$x4b', '$x5a', '$x5b', '$x5c', '$x6pl', '$x6ob', '$x7a', '$x7b', '$x8a', '$x8b','$x8c', '$x9', '$x10', '$x11a', '$x11b', '$x12', '$sum' )"));";
This returns me the parse error on the screen unexpecting t_string, expecting ; or,
Can you please look at my function add_result and try to tel me what is wrong with it.
Thanks, alenka
This is how i have putted into the database table:
ID bigint(20) not null auto_increment Spremeni Zavrži Primary Indeks Unique full text
I have tried to putt varchar but it did not work either
Did you mean to do the following:
echo "mysql_query( "insert into results values
('$valid_user', '$x1a', '$x1aa', '$x1b', '$x1bb', '$x2a', '$x2aa', '$x2b', '$x2bb', '$x2c', '$x2cc', '$x3a', '$x3b', '$x3c', '$x4a', '$x4b', '$x5a', '$x5b', '$x5c', '$x6pl', '$x6ob', '$x7a', '$x7b', '$x8a', '$x8b','$x8c', '$x9', '$x10', '$x11a', '$x11b', '$x12', '$sum' )"));";
No, I meant:
$query = "insert into results values ('$valid_user', '$x1a', ... )";
echo $query;
mysql_query($query);
In any case, the query is not valid. Since you don't want to insert the `ID` field, but let the database manage it, you must identify the columns that you do want inserted.
Please refer to h**p://www.mysql.com/doc/en/INSERT.html for details.
A quote from the above manual page:
If you specify no column list for INSERT ... VALUES or INSERT ... SELECT, values for all columns in the table must be provided in the VALUES() list or by the SELECT. If you don't know the order of the columns in the table, use DESCRIBE tbl_name to find out.
"INSERT INTO results (col1,col2,...) VALUES ('$valid_user', '$x1a', ...)"
Where (col1,col2,...) is a list of the field names you want to insert in the same order as the data you are inserting. Any field not listed will take it's default value. Use the same syntax for an UPDATE which allows you to do a partial record update.
The online manuals for MySQL and PHP are great resources for learning how to use them.
return true;
where: $x1a = $_POST['x1a'];...
I am realy confused. The only thing i changed from when it was working is that have added a field id into the databse table
I realy have no clue anymore.
Please help me if you can.
LP Alenka
But i still do not get it why did it work before?
Strange thing happen in my life every day.
Thanks to all of you for helping me. If someone knows what megic thing happend please explain to me.
lp Alenka