Get echo's in table top of page

Ask about a PHP problem here.
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Get echo's in table top of page

Post by GenSwat »

Learning php so far 2 that I wrote did all what I thought they would do, good!
now other issue, I want to learn how to get my echoes into a table that needs t be created at top of page left side.

here is the code.

[syntax=php]<?php
$numbers = strip_tags($_POST['pickanumber']);
$submit = $_POST['pickanumber'];

if($submit){
if (isset($_POST['pickanumber'])){
}
else{

}
if ($numbers>25||$numbers<1)
{
echo "<p>Enter a number between 1 and 25</p> ";
}
else{

}
if ($numbers==25)
{
echo "<font color='red'>"."<p>Congrats, Your a Winner!!!!</p>"."</font>";
}
else
{
echo "<p>Try Again,"."<font color='red'>You Lose!</font></p>";
}

}


?>
<html>
<form class="" action="guessanumber.php" method="POST">
<H2>Guess The Number</H2>
<h4>Guess The Number!</h4>
<br />
<div id="" style="margin-top:20px;">Pick a Number 1-25:</div><div id="" style="margin-top:20px;"><input name='pickanumber' class="" title="pickanumber" id='pickanumber' value="" size="20" maxlength="20" /></div>
<input type="submit" name="submit" value='pickanumber' class="pickanumber" />
</html>[/syntax]

As of now page jumps all over the place have hitting submit
also could you go over my code I did have some notice
One of my Favorites
Image
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Get echo's in table top of page

Post by ta2shop »

hy, here you can use strlen();
sould by:
[syntax=php]if (strlen($numbers)>25||strlen($numbers)<1)[/syntax]
alsow in the other $number variables.
alsow you mayte want to use
[syntax=php]
if(isset($submit)) {
}
[/syntax]
and , it is not very important to make a variable for everi thing you have, i mean $_POST or $_GET are variables!
you could just do
[syntax=php]
if(isset($_POST['submit'])) {
}
[/syntax]
and the same with everi thing else.
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Get echo's in table top of page

Post by Tino »

now other issue, I want to learn how to get my echoes into a table that needs t be created at top of page left side.


??

Just put a table before your form?

[syntax=php]<?php
if ( isset($_POST['submit']) ) {
// echo table with data using HTML table tag
}
?>
<form action="" method="post">
input stuff
</form>[/syntax]

Is that what you mean?

hy, here you can use strlen();


No!

Remember that he is entering numbers, not strings. Therefore he does not want to check the strlen there, just the value, like he did.



Other comments on your code:

First of all, don't use the font tag and try not to use inline styling. Use CSS and an external stylesheet instead.

Second, since you're submitting an integer, you can just cast it to an integer rather than using strip_tags.

[syntax=php]$number = (int) $_POST['number'];[/syntax]

Third, as ta2shop said, you don't need to store that submit variable in a new one. Just use $_POST['submit'] or whatever you called the submit button instead. Note that you do need to store the number in a new variable to cast it to an integer!
Please check out my CodeCanyon items.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Get echo's in table top of page

Post by ta2shop »

at lest i got somthing right :D
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Get echo's in table top of page

Post by jacek »

GenSwat wrote:As of now page jumps all over the place have hitting submit

I don't think it has been mentioned so far, but that is most likely caused by the output before the <html> tag.
Image
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Re: Get echo's in table top of page

Post by GenSwat »

Tino wrote:
now other issue, I want to learn how to get my echoes into a table that needs t be created at top of page left side.


??

Just put a table before your form?

[syntax=php]<?php
if ( isset($_POST['submit']) ) {
// echo table with data using HTML table tag
}
?>
<form action="" method="post">
input stuff
</form>[/syntax]

Is that what you mean?

YES

[syntax=php]$number = (int) $_POST['number'];[/syntax]

Third, as ta2shop said, you don't need to store that submit variable in a new one. Just use $_POST['submit'] or whatever you called the submit button instead. Note that you do need to store the number in a new variable to cast it to an integer!



Store the number in a new variable huh?
One of my Favorites
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Get echo's in table top of page

Post by Tino »

When you submit for example this form:

[syntax=xhtml]<form action="" method="post">
<input type="text" name="my_number" id="my_text" />
<input type="submit" value="Submit" />
</form>[/syntax]

Now, you might want to insert this into your database. So, for security reasons you need to make sure it's an integer (assuming you want an integer). This is done using (int). When you initially submit the form, the number is stored in $_POST['my_number'], but you have to store the $_POST['my_number'] converted to an integer in a new variable, or it won't work.

[syntax=php]$my_number = (int) $_POST['my_number'];[/syntax]

That's what I meant.

However, when you don't need to apply any security to it directly, say if you pass it to a function that applies the security or if it doesn't need to anywhere (like a potential submit button), you wouldn't need to do this.
Please check out my CodeCanyon items.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Get echo's in table top of page

Post by ta2shop »

hi means, instead of doing:
[syntax=php]
$submit = $_POST['submit'];
[/syntax]
just use:
[syntax=php]
$_POST['submit'];
[/syntax]
this is already a variable, you dont need a new one.
Image
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Re: Get echo's in table top of page

Post by GenSwat »

Ok I tried things like you said and it stopped working

here is some things I have done.

[syntax=php]<?php
$numbers = strip_tags($_POST['pickanumber']);
$submit = $_POST['submit'];
if (isset($_POST['submit']) && ($_POST['submit']))

if (isset($_POST['pickanumber'])&& ($_POST['pickanumber']))
if (!is_numeric($_POST['pickanumber'])) {
$error_tel = "<font color = 'red'>Enter only numbers please</font>";
echo $error_tel;
}
if($submit){
echo "<input type='text' size='30' name='pickanumber'>";

if($numbers && $submit){

if ($numbers>25||$numbers<1-1)
{
echo "<p>Enter a number between 1 and 25</p>";
}
else{

}
if ($numbers==25)
{
echo "<table border='1' width='200'>
<td><p>Congratulations, your a"."<font color='red'>winner!</font></p></td>
</table>";
}
else
{
echo "<table border='1' width='200'>
<td><p>Try Again,"."<font color='red'>You Lose!</font></p></td>
</table>";
}
}
}


?>
<html>
<head>
<link href="default.css" rel="stylesheet" type="text/css" />
</head>
<form class="" action="guessanumber1.php" method="POST">
<H2>Guess The Number</H2>
<h4>Guess The Number!</h4>
<br />
<div id="" style="margin-top:20px;">Pick a Number 1-25:</div><div id="" style="margin-top:20px;"><input name='pickanumber' class="" title='pickanumber' id='pickanumber' value='' size="20" maxlength="20" /></div>
<input type="submit" name="submit" value='submit' class="" />
</html>[/syntax]

you can see it atmy test site
http://genswat.dyndns.org/dashforcash/guessanumber1.php

I added some things just to learn how to not that it makes sense or looks nice
I added one css global so I wont add that till I want to pretty it up.

I get some undefine messages not sure but just hit submit and they go away. I checked if sets and echo'd out and they worked atleast what I think of is working. also if you type 0 in as an answer it does't follow my condition statement -1 and any other negitive number and 1-25 work, but zero just doesnt like being displayed lol

also I am not sure about the 'pickanumber' confused now *8( where does it come into play. is my script even using have of the lines... I am questiong myself now
Last edited by GenSwat on Tue May 10, 2011 12:41 pm, edited 1 time in total.
One of my Favorites
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Get echo's in table top of page

Post by Tino »

Wow, that code is a mess...

What you want to do is check if $_POST['pickanumber'] is set. If so, execute all the code.

[syntax=php]if ( isset($_POST['pickanumber']) ) {
$number = (int) $_POST['pickanumber'];

// etc.
}[/syntax]
Please check out my CodeCanyon items.
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Re: Get echo's in table top of page

Post by GenSwat »

Your telling me, I am depressed now.
One of my Favorites
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Get echo's in table top of page

Post by Tino »

With 'a mess' I mean it's very unreadable. You'll want to indent consistently to make it easier for us to help you, as well as to make your own life easier.
Please check out my CodeCanyon items.
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Re: Get echo's in table top of page

Post by GenSwat »

Tino wrote:With 'a mess' I mean it's very unreadable. You'll want to indent consistently to make it easier for us to help you, as well as to make your own life easier.




OK I re did it and indented not sure if this is clean or not any tips NoW my code works with no errors

[syntax=php]<?php

if (isset($_POST['submit']) && ($_POST['submit'])){
if (isset($_POST['pickanumber'])&& ($_POST['pickanumber'])){
$submit = $_POST['submit'];
$number = (int) $_POST['pickanumber'];
$error_tel = "<font color = 'red'>Enter only numbers please</font>";
if($number==$error_tel){
echo $error_tel;
}
if($submit){
echo "<input type='text' size='30' name='pickanumber'>";
if ($number>25||$number<1-1)
{
echo "<p>Enter a number between 1 and 25</p>";
}
else{
echo("");
}
if ($number==25)
{
echo "<table border='1' width='200'>
<td><p>Congratulations, your a "."<font color='red'>winner!</font></p></td>
</table>";
}
else
{
echo "<table border='1' width='200'>
<td><p>Try Again,"."<font color='red'>You Lose!</font></p></td>
</table>";
}
}
}
}

?>
<html>
<head>
<link href="default.css" rel="stylesheet" type="text/css" />
</head>
<form class="" action="guessanumber1.php" method="POST">
<H2>Guess The Number</H2>
<h4>Guess The Number!</h4>
<br />
<div id="" style="margin-top:20px;">Pick a Number 1-25:</div><div id="" style="margin-top:20px;"><input name='pickanumber' class="" title='pickanumber' id='pickanumber' value='' size="20" maxlength="20" /></div>
<input type="submit" name="submit" value='submit' class="" />
</html>[/syntax]
I need sleep still up since yesterday php make you nuts
One of my Favorites
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Get echo's in table top of page

Post by Tino »

That's really just as bad...

This is how I would do it, just so you have an example of what you could do. Note that there is no perfect way of styling your code. If you have trouble coming up with a good style, you could always adopt a coding standard like Zend's or PEAR's.

[syntax=php]<?php
if ( isset($_POST['submit']) && ($_POST['submit']) ) {
if ( isset($_POST['pickanumber']) && ($_POST['pickanumber']) ) {
$submit = $_POST['submit'];
$number = (int) $_POST['pickanumber'];
$error_tel = '<font color="red">Enter only numbers please.</font>';

if ( $number == $error_tel ) {
echo $error_tel;
}

if ( $submit ) {
echo '<input type="text" size="30" name="pickanumber" />';

if ( $number > 25 || $number < 1 - 1 ) {
echo '<p>Enter a number between 1 and 25</p>';
} else {
echo '';
}

if ( $number == 25 ) {
echo '<table border="1" width="200">';
echo '<td><p>Congratulations, you are a <font color="red">winner!</font></p></td>';
echo '</table>';
} else {
echo '<table border="1" width="200">';
echo '<td><p>Try again. <font color="red">You lose!</font></p></td>';
echo '</table>';
}
}
}
}
?>[/syntax]

That said, your code makes no sense. Now that you have an easier to read version of your code, can you spot some things yourself?
Please check out my CodeCanyon items.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Get echo's in table top of page

Post by jacek »

[syntax=php]echo '';[/syntax]
This is interesting ;)

On the style thing though, it really does not matter how you do it, but indenting things in a consistent way that works for you will defiantly help.

Like Tino's example all of the code inside each block is tabbed in by one.
Image
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Re: Get echo's in table top of page

Post by GenSwat »

jacek wrote:[syntax=php]echo '';[/syntax]
This is interesting ;)

On the style thing though, it really does not matter how you do it, but indenting things in a consistent way that works for you will defiantly help.

Like Tino's example all of the code inside each block is tabbed in by one.



LOL wondering if I could just get rid of it. or wasnt sure if I could run another if under it, so I left the else as
[syntax=php]echo '';[/syntax]

well now I look at it I did it above why not delete it.

man all this[syntax=php]<?php
foreach($this as $that) if(){} and else{echo that} elseif {this or that}
which one where and when...

So I get lost and worried about it
?>[/syntax]
One of my Favorites
Image
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Re: Get echo's in table top of page

Post by GenSwat »

Tino wrote:That's really just as bad...
That said, your code makes no sense. Now that you have an easier to read version of your code, can you spot some things yourself?



man and I thought I was getting some where.

I must not understand it as well as I thought.

ok here is what I was doing.

I started out. let me make a input field and submit button. I want it to sumit a value and check to make sure that the value was an (int) only. Then need it to check if was submited,so I used the if( $submit ),then if the value entered = 25 message congrats.
If not it would check to make sure atleast values used were 1-25, if not another message (num between 1-25).
The table was to see/try to make a table each with message as well as a text field echo'd through php

the [syntax=php]$numbers = $_POST['pickanumber'];[/syntax] well I am thinking maybe only needed if I am going to use a database

not real sure now after your critism...
One of my Favorites
Image
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Re: Get echo's in table top of page

Post by GenSwat »

After feeling like I was kicked when I was down, LOL

I accepted the critism as constructive. I went back to the books and took your format into my page.
You was correct page didnt make sense half of that stuff is not necessary for the page to function
So I went back threw and cleaned up the best I could you thought now? Thanks Tino, I appreciate your time on proof reading and correction. I even went a step further and made a seperate function.php and wrote my first function for error msg.
also a require to functions.php

here is my new version know its better adding your indents how does it look now with the add-ons?

[syntax=php]<?php

require ('function.php');

if (isset($_POST['submit']) ) {
$submit = $_POST['submit'];
$number = (int) $_POST['pickanumber'];
$error_tel = "<font color = 'red'>Enter only numbers please</font><br />";

if( $number==$error_tel ){
echo $error_tel;
}

if($submit){
//echo "<input type='text' size='30' name='pickanumber'>";

if ($number>25||$number<1-1) {
echo "<p>Enter a number between 1 and 25</p><br />";
} else {
echo '';
}

if ($number==25) {
echo '<table border="1" width="200">';
echo '<td><p>Congratulations, your a '.'<font color="red">winner!</font></p></td>';
echo '</table>';
} else {

echo myMessage('error');
}
}
}
?>
<html>

<head>

</head>

<form class="" action="guessanumber1.php" method="POST">

<h2>Guess The Number<h2>
<h4>Guess The Number!</h4>

<br />

<div id="" style="margin-top:20px;">Pick a Number 1-25:</div>
<div id="" style="margin-top:20px;">
<input name='pickanumber' size="20" maxlength="20" /></div>
<input type="submit" name="submit" value='submit' />
</html>[/syntax]

Could show me how you would do the html indentation. TY
[syntax=php]if(!empty($_POST['submit'])) { [/syntax]


[syntax=php]if (isset($_POST['submit']) && ($_POST['submit'])) {
[/syntax]

What would be the case in my script. Does it matter? Which is better used?
One of my Favorites
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Get echo's in table top of page

Post by Tino »

What would be the case in my script. Does it matter? Which is better used?


You would use

[syntax=php]if ( isset($_POST['submit']) ) { }[/syntax]

As for the rest of your code:

[syntax=php]$submit = $_POST['submit'];[/syntax]

There is absolutely no need for this line. You could very well just use $_POST['submit'].

Then, when you go to define $error_tel, you use a font tag. Don't use font tags!

[syntax=php]if( $number==$error_tel ){
echo $error_tel;
}[/syntax]

This is a very big logical error. You cast number to an integer, meaning it's a number. Then you set the error_tel to some message. In other words, this won't ever be the same. Because you are already casting it to an integer, there really is actually no need for this if statement at all. Get rid of it, and with that, the $error_tel variable.

Next line you have if (s$submit). This is pointless. You start the file checking if $_POST['submit'] is set, and now you check for that again. Get rid of this.

[syntax=php]if ($number>25||$number<1-1) {
echo "<p>Enter a number between 1 and 25</p><br />";
} else {
echo '';
}[/syntax]

This is logically very weird. First of all, ditch that empty echo. Second, why do you check if it's less than 1 - 1, which is 0? You want it to be between 1 and 25, so you should just check if it's less then 1.

Aside of that, don't use tables for layout purposes, like you do in the confirmation message.
Please check out my CodeCanyon items.
User avatar
GenSwat
Posts: 74
Joined: Sat May 07, 2011 3:37 pm

Re: Get echo's in table top of page

Post by GenSwat »

WoW, what was I thinking!!! :shock:
[syntax=php]<?php

require ('function.php');

if (isset($_POST['submit']) ) {
$number = (int) $_POST['pickanumber'];

if ($number>25||$number<1) {
echo "Enter a number between 1 and 25 <br />";
}

if ($number==25) {
echo "Congratulations, your a winner";
} else {
echo myMessage('error');
}
}
?>
<html>

<head>

</head>

<form action="guessanumber1.php" method="POST">

<h2>Guess The Number<h2>

<br />

<div id="" style="margin-top:20px;">Pick a Number 1-25:</div>
<div id="" style="margin-top:20px;">
<input name='pickanumber' size="20" maxlength="20" /></div>
<input type="submit" name="submit" value='submit' />

</form>
</html>

[/syntax]

I thinking,every thing you said is making sense now.That is one of my problems making it harder the it really is.

Thank You. well I think it is looking better now, any more code removed its gonna get a "$" fine for indecesant exposure.


wow I wasted alot of time and yours with all that junk.
One of my Favorites
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Get echo's in table top of page

Post by Tino »

No you didn't, because you just learned a valuable lesson. And knowing my advise helped you is good for me too :)

And that code now looks good to me.
Please check out my CodeCanyon items.
Post Reply