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
259 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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 …

828 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