Jump to content


Photo

Default button to 'no'


  • Please log in to reply
20 replies to this topic

#1 michellephp

michellephp
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 28 March 2006 - 06:30 AM

Hello again!

I am now having another little problem with the realestate script!

I have a button (in the admin area) that says (well the heading says) "sold?". Then the button says either yes or no after clicking on it. However if it hasn't been clicked on yet, it comes up as a blank button. I want it to come up as 'no' (without having to click) - because if you've just listed a property, it's not likely to already be sold!

So is it possible to do this? I think I can make it come up saying no, but then it must be click on twice to get to yes (as the first click still corresponds to 'no').

<button onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'"><? echo $row_p['sold']; ?></button>

And this is the code in the sold.php:

//update sold

if($_REQUEST['id'] != "") {
$sql = "select * from items where id = ". $_REQUEST['id'];
$result = mysql_query($sql,$myconn) or die(mysql_error());

if (mysql_num_rows($result) > 0) {// if found

//fetch results in assoc array
$row_item = mysql_fetch_assoc($result);

if ($row_item['sold'] == "no")
$sold = "Yes";
else
$sold = "No";

//build update sql string
$update = "update items set sold = '".$sold."' where id = ".$_REQUEST['id'];
$result = mysql_query($update,$myconn) or die(mysql_error());

Thanks again everyone :) :)
Michelle.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 March 2006 - 11:03 AM

Does this work?

<?php 
       $YorN = $row_p['sold']=='no' ? 'No' : 'Yes';
?>

<INPUT TYPE='BUTTON'  name='sold' value='<?php echo $YorN ?>' onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'">

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 michellephp

michellephp
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 28 March 2006 - 01:17 PM

Thanks for the suggestion :) but it didn't seem to fix it. Now, the button automatically displays 'yes' (i want 'no') and when click it still says yes and doesn't update the db.

i'm totally stumped!

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 March 2006 - 01:39 PM

Try slightly different tack
<?php 
       $YorN = $row_p['sold'] != 'Yes' ? 'No' : 'Yes';
?>

<INPUT TYPE='BUTTON'  name='sold' value='<?php echo $YorN ?>' onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'">

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 norman100

norman100
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 28 March 2006 - 01:47 PM


<button onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'"><? echo $row_p['sold']; ?></button>

Michelle, i think that you will need a IF statement in here ,

<button onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'">

<?

$status = $row_p['sold'];

IF (!$status) {

$status = 'No'

ELSE

//WE HAVE A VALUE

$status=$row_p['sold'];

}

?></button>



[!--quoteo(post=359255:date=Mar 28 2006, 08:17 AM:name=michellephp)--][div class=\'quotetop\']QUOTE(michellephp @ Mar 28 2006, 08:17 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thanks for the suggestion :) but it didn't seem to fix it. Now, the button automatically displays 'yes' (i want 'no') and when click it still says yes and doesn't update the db.

i'm totally stumped!
[/quote]


#6 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 28 March 2006 - 01:53 PM

Which is the long way of doing what Barand suggested...

[!--quoteo(post=359261:date=Mar 28 2006, 08:47 AM:name=norman100)--][div class=\'quotetop\']QUOTE(norman100 @ Mar 28 2006, 08:47 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
<button onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'"><? echo $row_p['sold']; ?></button>

Michelle, i think that you will need a IF statement in here ,

<button onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'">

<?

$status = $row_p['sold'];

IF (!$status) {

$status = 'No'

ELSE

//WE HAVE A VALUE

$status=$row_p['sold'];

}

?></button>
[/quote]


#7 michellephp

michellephp
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 28 March 2006 - 03:34 PM

Hi everyone,

thanks for your replies!

I tried both options but sadly neither worked. The first made the button say 'no', but when clicked did not then say 'yes', nor was the db updated.

The second gave me this error: Parse error: syntax error, unexpected T_ELSE

I think (but really have no idea) that the problem may be that the propety hasn't got a sold status, until the button is clicked. Then if it is clicked once, the staus is then 'no'. Clicked again and the status is 'yes'. Clicked again, back to 'no', etc etc.

My main problem with it is that it looks a bit dodgy having a button with no writing in it. Maybe, if I had raido buttons, one for yes, and one for no, it would look passable. But I've no idea how to make the script adapt to that. Or maybe just a single little radio button they could click once it has sold. So that would mean the property never has the status 'no' (ie not sold) just 'yes'. Unless the button is unclicked... then the status would be no. But that's pretty much the same as what I've got............ although I think the empty radio button would look better than the empty button. I'll try to make it into a radio button instead :) If anyone knows how to do it off the top of their head please feel free to show me :) :) Otherwise I'll play around till I get it.

Thanks!



#8 norman100

norman100
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 28 March 2006 - 03:49 PM

Michelle!

Thats not even a problem

add me to msn

nokia_buyer@hotmail.com

or email me

and we will discuss this

nirmal



[!--quoteo(post=359313:date=Mar 28 2006, 10:34 AM:name=michellephp)--][div class=\'quotetop\']QUOTE(michellephp @ Mar 28 2006, 10:34 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi everyone,

thanks for your replies!

I tried both options but sadly neither worked. The first made the button say 'no', but when clicked did not then say 'yes', nor was the db updated.

The second gave me this error: Parse error: syntax error, unexpected T_ELSE

I think (but really have no idea) that the problem may be that the propety hasn't got a sold status, until the button is clicked. Then if it is clicked once, the staus is then 'no'. Clicked again and the status is 'yes'. Clicked again, back to 'no', etc etc.

My main problem with it is that it looks a bit dodgy having a button with no writing in it. Maybe, if I had raido buttons, one for yes, and one for no, it would look passable. But I've no idea how to make the script adapt to that. Or maybe just a single little radio button they could click once it has sold. So that would mean the property never has the status 'no' (ie not sold) just 'yes'. Unless the button is unclicked... then the status would be no. But that's pretty much the same as what I've got............ although I think the empty radio button would look better than the empty button. I'll try to make it into a radio button instead :) If anyone knows how to do it off the top of their head please feel free to show me :) :) Otherwise I'll play around till I get it.

Thanks!
[/quote]


#9 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 28 March 2006 - 04:04 PM

Norman,

Normally, we'd like to keep these discussions online so that other people searching through the forums can find answers as well. I obviously can't stop either of you from proceding, but I also think it should be the origional poster's position to ask about taking it offline.

Info: PHP Manual


#10 norman100

norman100
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 28 March 2006 - 04:19 PM

Hi

In the first instance try the following





<button onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'">

<?



IF (!$status) {

$status = 'No'

echo $status;


}


ELSE {

//WE HAVE A VALUE

$status=$row_p['sold'];

echo $status;


}

?></button>

Ober no you misunderstood me, I just wanted to discuss the problem in more detail perhaps even see the underlying mysql database,

I felt that i couldnt get this same level of communication through the forum,

But should that be your wish that it all stays online then thats fine.

#11 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 28 March 2006 - 04:44 PM

No offense, but I don't think anyone on here is going to (or at least they shouldn't) just show you their MySQL database. That's a security risk. If you can't figure out the problem, then leave it for someone else to answer or ask the poster to provide more information that they're comfortable sharing.

Info: PHP Manual


#12 michellephp

michellephp
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 28 March 2006 - 04:58 PM

Hi Norman,

Thanks for the suggestion :) however this time I got the error:
Parse error: syntax error, unexpected T_ECHO

Maybe the radio buttons would be easier?



#13 michellephp

michellephp
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 31 March 2006 - 04:58 PM

Hi everyone again!

I have partially fixed this, by adding the 'if' as suggested. However I still have a problem. It comes up as 'no' is there has been no value assigned for it. It comes up as yes if it has 'yes' assigned to it. But still, the person has to click the 'no' button twice to get to use.

I.e.
First No = "no"
second No = "yes"

Code:
if ($row_p['sold'] == "Yes")
$sold = "Yes";
else
$sold = "No"; ?><button onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'" value="No"><? echo $sold; ?></button>

And the sold.php:

if($_REQUEST['id'] != "") {
$sql = "select * from items where id = ". $_REQUEST['id'];
$result = mysql_query($sql,$myconn) or die(mysql_error());

if (mysql_num_rows($result) > 0) {// if found

//fetch results in assoc array
$row_item = mysql_fetch_assoc($result);

if ($row_item['sold'] == "No")
$sold = "Yes";
else
$sold = "No";

//build update sql string
$update = "update items set sold = '".$sold."' where id = ".$_REQUEST['id'];
$result = mysql_query($update,$myconn) or die(mysql_error());

Now logically, you would think that you would change the "no" to "yes" in the button link. And then the no in
"if ($row_item['sold'] == "No")" to yes. BUt when I do that, when the button is clicked it doesn't change tp yes (even if clicked twice) and doesn't update the db.

I thought maybe by adding an ORDER BY somewhere (because my only thought at the moment is that "no" is always the first option submitted as it comes first in the alphabet....). But if this might be the case, where do I stick the ORDER BY desc??

Thanks again,
Michelle

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 March 2006 - 05:34 PM

Change the following:
<button onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'" value="No"><? echo $sold; ?></button>
to this:
<input type="button" onclick="window.location='sold.php?id=<? echo $row_p['id']; ?>'" value="<?php echo $sold; ?>">


#15 michellephp

michellephp
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 31 March 2006 - 05:46 PM

Hi,

Thanks for your help. Unfortunately it still doesn't work; the button still needs to be clicked twice.

It seems to me that the form is not passing on the value "no".

Michelle :)

#16 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 31 March 2006 - 06:43 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]It seems to me that the form is not passing on the value "no". [/quote]

The button is passing the id of the property. The record with that id contains the "sold" value, Yes or No. So you should be able to determine whether property is sold or not.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#17 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 31 March 2006 - 11:46 PM

I got 1 hour of sleep last night so bare with me, im not sure i understand.

Your page loads a listing of houses, and each of them has a button, that you would like to say 'no', but when you click, you want it to display 'yes'?

If so, why not have a column in your database, let's say "sold", that can hold either "yes" or "no".
Then, you could retrieve it and put it as the button value like

<input type="submit" value="<?php echo $row['sold']; ?>">

Is that it? sorry if im way off.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#18 michellephp

michellephp
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 01 April 2006 - 05:01 AM

Hi,

the buttons are in the backend, where the people can add their homes to be sold. There's a button in one of teh columns that they can click to say whether or not the property has sold.

At the moment, the button initially comes up empty (which I think looks dodgy). Then if you click it once, it says 'no' and the database knows it is not sold (although if the button has never been pressed the database does not say the property has been sold either).
If you click it again, the button says 'yes'.

But really, if the person has just listed their property, it won't be sold yet. So the button should come up saying 'no' rather than blank. And then, the person should only have to click it once to say the property has been sold = "yes"

At the moment, I have it so the button comes up saying "no" which looks better. But the person still has to click it twice to get to "yes"
ie
comes up saying (but with no real value) = "no"
first click = "no"
2nd click = "yes"

so if the db could assume if not yes, = no, that would be good. But I can't get it to do that.

#19 michellephp

michellephp
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 01 April 2006 - 04:25 PM

Hi Barand,

I'm not sure I understand... what does that mean?

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]So you should be able to determine whether property is sold or not.[/quote]

It is working so far as it updates the db... it's just when the button is clicked it updates the db to 'no' first (i want 'yes' first).

I thought maybe this was happening because of this line is sold.php

$update = "update items set sold = '".$sold."' where id = ".$_REQUEST['id'];

See i thought there should be another " at the end.... but I get this error when I add one:
Parse error: syntax error, unexpected '"' in /home/homebuy/public_html/sold.php on line 23

But when I take the " out (ie where id = .$_REQUEST)
I get: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /public_html/sold.php on line 23

Do you think this could have something to do with it?

#20 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 01 April 2006 - 06:41 PM

I've set a simple sample system similar to yours. Hope it helps

The table
CREATE TABLE property (
        propID int not null auto_increment primary key,
        descrip varchar(20),
        price int,
        sold varchar(3)
        );

The data
INSERT INTO property (descrip, price) VALUES
('a house', 220000),
('big house', 354995),
('huge house', 1295000),
('bungalow', 295000),
('studio apartment', 140000);

The code
<?php
include 'db.php';
// -- supporting functions ------------------------------------------------------------------
function listProperties() {
         $sql = "SELECT propID, descrip, price, sold
                 FROM property
                 ORDER BY price ";
         $res = mysql_query($sql) or die (mysql_error());
         echo "<TABLE width='500' cellspacing='0' cellpadding='5'>\n";
         echo   "<TR>
                  <TH>ID</TH>
                  <TH>Property</TH>
                  <TH>Price</TH>
                  <TH>Sold</TH>
                  </TR>\n";
         while (list($id, $desc, $price, $sold) = mysql_fetch_row($res)) {
                $btnval = ($sold=='Yes') ? 'Yes' : 'No';
                $script = $_SERVER['PHP_SELF'];
                $btn = "<INPUT type='button' value='$btnval' onClick='location.href=\"$script?sellid=$id\"'>";
                echo   "<TR>
                  <TD>$id</TD>
                  <TD>$desc</TD>
                  <TD style='text-align:right'>" . number_format($price,0) . "</TD>
                  <TD style='text-align:center'>$btn</TD>
                  </TR>\n";

         }
         echo "</TABLE>\n";
}

function sellProperty($id) {
         $sql = "SELECT sold FROM property
                 WHERE propID = '$id'";
         $res = mysql_query($sql) or die (mysql_error());
         $sold = mysql_result($res, 0);
         $sellval = ($sold=='Yes' ? 'No' : 'Yes');
         mysql_query ("UPDATE property SET sold = '$sellval' WHERE propID = '$id'");
}
// --  end functions ------------------------------------------------------------------

if (isset($_GET['sellid'])) {
    sellProperty($_GET['sellid']);
}


?>
<HTML>
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<meta Name="generator" content="PHPEd Version 3.1.2 (Build 3165)">
<title>Sample property page</title>
<meta Name="author" content="Barand">
</HEAD>
<BODY>

<?php

     listProperties();

?>

</BODY>
</HTML>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users