Solved

the code works when there are id's linewise means1,2,3,4 otherwise it gives error,please help me out.

Posted on 2009-05-05
6
251 Views
Last Modified: 2013-12-12
this code works when i have the id's line wise, can u modify it please. whatever be the id ,it should flip the records accordingly.

in this code, i have records in the database. the user will set the order of the items to be displayed by entering in the textbox. please help me out, i am trying but not getting the result what i wanted.
<form name="form" action="2.php" method="post">

<input type="submit" name="submit" id="submit" value="save" /><br />
 

<?php

$con=odbc_connect('try','','');
 

$sql="select * from tablename1 order by id";

$query=odbc_exec($con, $sql);

while (odbc_fetch_row($query))

{	//echo "t".$query['id'];exit;

	echo $id="t".odbc_result($query,"id");

	//exit;

	?>

		<input type="text" name="<?php echo "t".odbc_result($query,"id"); ?>" />

		<a href="2.php?id=<?php echo $id=odbc_result($query,"id"); ?>"><?php echo $name=odbc_result($query,"name"); ?></a>

	<br /><br />

	<?php

}
 

	if(isset($_POST['submit'])){

		  for($i=1;$i<count($_POST);$i++){

		  	//echo $_POST["t".$i]."<br>";

			$con=odbc_connect('try','','');

			$sql_command = "select name from tablename1 where id=$i";

			$rs_person = odbc_exec($con, $sql_command);

			$person = odbc_fetch_row($rs_person);

			$c[$_POST["t".$i]]= odbc_result($rs_person,"name");

			//$c[$_POST["t".$i]]=$person['name'];

		  }

		  

		  

		  for($i=1;$i<count($_POST);$i++){

				echo $c[$i]."<br>";		// echo in this fashion $a[1], $a[2] and so on in the order wise

		  }

		  

		  //echo count($_POST)-1;exit;

		  for($i=1;$i<count($_POST);$i++){

		  		//echo $i."<br>";

				//echo $_POST["t".$i]."<br>";

				$con=odbc_connect('try','','');

				$sql_command1 = "update tablename1 set `order1`=". $_POST["t".$i]." where id=$i";

				$ok=odbc_exec($con, $sql_command1);

				//echo "<br><br>";

				echo "<br>";

		  }

	}

?>

</form>

Open in new window

0
Comment
Question by:designersx
  • 4
  • 2
6 Comments
 

Author Comment

by:designersx
ID: 24302439
here in this i need to apply the isset condition that if there is no record in the database, then it should not execute that statement.

i am trying but can u look at it. like this. see 24 and 39 line.
<form name="form" action="3.php" method="post">

<input type="submit" name="submit" id="submit" value="save" /><br />
 

<?php

$con=odbc_connect('db1','','');
 

$sql="select * from Table1 order by id";

$query=odbc_exec($con, $sql);

while (odbc_fetch_row($query))

{	//echo "t".$query['id'];exit;

	echo $id="t".odbc_result($query,"id");

	//exit;

	?>

		<input type="text" name="<?php echo "t".odbc_result($query,"id"); ?>" />

		<a href="2.php?id=<?php echo $id=odbc_result($query,"id"); ?>"><?php echo $name=odbc_result($query,"name"); ?></a>

	<br /><br />

	<?php

}
 

	if(isset($_POST['submit'])){

		  for($i=1;$i<count($_POST);$i++){

		  	//echo $_POST["t".$i]."<br>";

			$con=odbc_connect('db1','','');

			if(isset($_REQUEST["t".$i])){

				$sql_command = "select name from Table1 where id=".$i;

			}	

			$rs_person = odbc_exec($con, $sql_command);

			$person = odbc_fetch_row($rs_person);

			$c[$_POST["t".$i]]= odbc_result($rs_person,"name");

			//$c[$_POST["t".$i]]=$person['name'];

		  }

		  

		  for($i=1;$i<=count($_POST);$i++){

				echo $c[$i]."<br>";		

		  }

		  

		  for($i=1;$i<count($_POST);$i++){

				$con=odbc_connect('db1','','');

				if(isset($_REQUEST["t".$i])){

					echo $sql_command1 = "update Table1 set `order1`=". $_POST["t".$i]." where id=".$i;

				}

				$ok=odbc_exec($con, $sql_command1);

		}

	}

?>

</form>

Open in new window

0
 
LVL 14

Accepted Solution

by:
agriesser earned 125 total points
ID: 24302575
OK, to be honest, I have no idea what you're trying to do here, but I have corrected some things in your code now, although it probably will not work.

As far as i understood, there's a a table "tablename1" with at least two attributes called "id" and "name" in there.

id is a simple number (1, 2, 3 or something like that)
name is any name for a person or something like that.

you are reading all records from this table and order them by the id (ascending) and get the id and name from these records.
Then, you create input fields in the given order from these records, the input fields should be named like "tID", so to speak, the letter "t" is prefixed to the "ID" gathered from the database, f.ex. id 2 will be a textfield with name "t2".

Then you display the name of this id in a link that points to a script 2.php with parameter id=t2 (if we stick to our example from above).

If the user has hit the submit button, you walk through all the post variables to find out what ids have been set in prior.
i changed this a bit and will only consider post variables that start with a "t" and have a number behind that.

You (again) retrieve the name for this id out of the database and fill an associateive array $c with the names of these persons.

Then, you output all the names in the given order to the screen.

Finally, you do some updates on the table tablename1 attribute "order1" with the names you go before, but I don't understand that part.
What should be listed then in "order1"?
How should that look like?

<form name="form" action="2.php" method="post">

<input type="submit" name="submit" id="submit" value="save" /><br />
 

<?php

$con=odbc_connect('try','','');
 

$sql="select * from tablename1 order by id";

$query=odbc_exec($con, $sql);

while (odbc_fetch_row($query))

{       //echo "t".$query['id'];exit;

        $id="t".odbc_result($query,"id");

        $name = odbc_result($query, "name");

        echo '<input type="text" name="' . $id . '" />

                <a href="2.php?id=' . $id . '">' . $name . '</a>

        <br /><br />';

}

if(isset($_POST['submit'])){

  foreach($_POST as $key => $value)

  {

    if(preg_match('/^t[0-9]+/', $key))

    {

      $sql_command = "select name from tablename1 where id=$i";

      $rs_person = odbc_exec($con, $sql_command);

      $person = odbc_fetch_row($rs_person);

      $c[$key]= odbc_result($rs_person,"name");

      echo $c[$key]."<br>";             // echo in this fashion $a[1], $a[2] and so on in the order wise

    }

  }

  foreach($c as $key => $value)

  {

    $sql_command1 = "update tablename1 set `order1`=". $c[$key]." where id=$i";

    $ok=odbc_exec($con, $sql_command1);

     echo "<br>";

   }

  }

?>

</form>

Open in new window

0
 

Author Comment

by:designersx
ID: 24302940
please see

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24365101.html


this was the question asked by me earlier but i closed it by mistake. i have done it 90% .

i am just setting the order of the items to be displayed on the front end and then save that order in the database in the field order1.

simply, i want to show that record first which has got priority 1 and so on.

ok , let me try ur code.

thanks
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:designersx
ID: 24302970
lots of error

Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'id='., SQL state 37000 in SQLExecDirect in C:\wamp\www\fantaay\try\4.php on line 23

Warning: odbc_fetch_row(): supplied argument is not a valid ODBC result resource in C:\wamp\www\fantaay\try\4.php on line 24

Warning: odbc_result(): supplied argument is not a valid ODBC result resource in C:\wamp\www\fantaay\try\4.php on line 25

0
 

Author Comment

by:designersx
ID: 24303060
able to solve the code,don't take tension. thanks buddy for the pains u take for me.

thanks again.
0
 
LVL 14

Expert Comment

by:agriesser
ID: 24303261
OK, so, does it work now for you or is there anything left that doesn't work as expected?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now