|
-
May 19th, 2009, 08:35 AM
#1
Thread Starter
Addicted Member
[RESOLVED] generating multiple rows in a Table Problem
Whats gone wrong here?
PHP Code:
<?PHP //URL array organised
$lookatURL = $_GET["item"];
$URLarray = explode(":",$lookatURL);
$URLnumber = count($URLarray);
?>
<table width="100%" border="1" cellspacing="2" cellpadding="0">
<tr>
<td width="10%" align="center" valign="top">Item</td>
<td width="80%" align="center" valign="top">
Description</td>
<td width="10%" align="center" valign="top">Total</td>
</tr>
<?PHP
$boxRows = 0;
while ($boxRows < $URLarray[1]){
?><tr>
<td width="10%"><form name="form1" id="form1">
<select name="item" onchange="MM_jumpMenu('parent',this,0)">
<option>Select</option><?PHP
$trees = (mysql_query("SELECT * FROM database"));
while($row = mysql_fetch_array($trees)){
?><option value="?item=<?PHP
$numbered = 0;
while ($numbered < $URLnumber )
{
$bx = ($boxRows + 2);
$URLarray[$bx] = $row[3];
echo "$URLarray[$numbered]:";
$numbered++;
}
?>"><?PHP echo "$row[3]"; ?></option>
<?PHP } ?>
</select>
</form></td>
<td width="80%"></td>
<td width="10%"></td>
</tr>
<?PHP
$boxRows++;
} ?>
<tr>
<td><a href="?item=<?PHP
$boxNumber = ($URLarray[1]+1);
$URLarray[1] = $boxNumber;
$numbered = 0;
while ($numbered < $URLnumber )
{
echo "$URLarray[$numbered]:";
$numbered++;
}
?>" class="style4">add</a></td>
<td class="style2"><span class="style4"></span></td>
<td></td>
</tr>
</table>
<?PHP
mysql_close($con);
?>
I can use the code above to generate new rows in a table, by using the add text link. Information from the database is put into drop down menus. The links in the menus are used to create a ?item= URL link on the page.
Each link has been seporated to fill the information in along different parts of the URL.
When selecting choices from the bottom and moving up the generated rows every thing in the URL displays as expected. But if done in reverse selecting from the top and moving down the choices, everything changes to the last row in the database.
Any additions to this code to solve this problem?
Thanks.
-
May 19th, 2009, 02:28 PM
#2
Re: generating multiple rows in a Table Problem
do you have a working example so that I can look and actually see what's going on? since all of this seems to sort of depend on the data in your table, it would be hard for me to construct my own. if you're not comfortable sharing, PM me it if you'd like.
-
May 20th, 2009, 01:43 AM
#3
Thread Starter
Addicted Member
Re: generating multiple rows in a Table Problem
You can see the code in practice here...
http://arborealservices.com/testing/quote.php
1.click add to add rows(have to click twice first time)
2.use the pulldown menu to select the item
3.view the result in the URL
Im looking to add information from the database into the other cells later by identifing whats writen in the URL.
Thanks kows
-
May 20th, 2009, 01:54 AM
#4
Re: generating multiple rows in a Table Problem
erm, when I added anything the <select> was completely empty. is this a problem with the code or just your script? or did you not put any data in the table on the database? I still can't see what's going on, though.
edit: upon viewing the source, it looks like you just didn't create that table or do anything with it, as it's returning an error:
Code:
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/home/content/k/w/i/kwikchip/html/testing/quote.php</b> on line <b>79</b><br />
-
May 20th, 2009, 02:18 AM
#5
Thread Starter
Addicted Member
Re: generating multiple rows in a Table Problem
Sorry i was linking to the wrong table, Doh!
If you look now it works in the manner it was before.
-
May 20th, 2009, 05:29 AM
#6
Re: generating multiple rows in a Table Problem
I'm having a bit of a hard time understanding what exactly you're trying to accomplish, but I think I at least recreated your problem. either way, I think you could much easier accomplish this using arrays of some kind instead of trying to manipulate the URL yourself. I'm very tired, but can probably make some kind of example after I'm done sleeping, if you're not sure where to start!
-
May 20th, 2009, 06:25 AM
#7
Thread Starter
Addicted Member
Re: generating multiple rows in a Table Problem
Thanks kows,
I don't know how to capture user inputs without using the url!
Is it possible?
-
May 20th, 2009, 02:55 PM
#8
Re: generating multiple rows in a Table Problem
yes, you'll still be using the URL, but not in the way that you are. I've made a quick example of how I might do what you're doing, and it shouldn't take too much work to apply it to using your database instead of my test array. you can see an example of it here, but let me know if that was the functionality you were looking for! hopefully, the code isn't too confusing.
PHP Code:
<hr /> <?php //our array $array = array("Apple", "Orange", "Banana");
if(count($_GET['items'])){ //sort them so they are in the correct order, at least visually. ksort($_GET['items']);
//loop through our array and let the user select an item foreach($_GET['items'] as $key => $value): ?> <form action="quotes.php" method="get"> <!-- previous items --> <?php if(count($_GET['items'])){ foreach($_GET['items'] as $rkey => $rvalue): if($rkey == $key) continue; ?> <input type="hidden" name="items[<?php echo $rkey; ?>]" value="<?php echo htmlentities($rvalue); ?>" /> <?php endforeach; } ?> <!-- /previous items --> <select name="items[<?php echo $key; ?>]"> <option value=""></option> <?php foreach($array as $option): ?> <option value="<?php echo $option; ?>"<?php if($value == $option) echo ' selected="selected"'; ?>><?php echo $option; ?></option> <?php endforeach; ?> </select> <input type="submit" value="Go" /> </form> <hr /> <?php endforeach; } ?>
<form action="quotes.php" method="get"> <!-- previous items --> <?php if(count($_GET['items'])){ foreach($_GET['items'] as $key => $value): ?> <input type="hidden" name="items[<?php echo $key; ?>]" value="<?php echo htmlentities($value); ?>" /> <?php endforeach; } ?> <!-- /previous items --> <!-- new item --> <input type="hidden" name="items[<?php echo count($_GET['items']); ?>]" value="" />
<input type="submit" value="Add" /> </form>
basically, it's the same concept you have but it's using actual forms instead of building a URL (although building a URL with the same method is just as easy, but this way it's sort of done for you). to mimic the functionality you have currently, you could simply make the forms submit on change of the <select> value.
let me know!
-
May 26th, 2009, 06:12 AM
#9
Thread Starter
Addicted Member
Re: generating multiple rows in a Table Problem
I've taken a step back and this is where I am at the moment...
PHP Code:
<?php
$lookatURL = $_GET["item"];
$URLarray = explode(":",$lookatURL);
$retreveA=$_REQUEST['ID'];
$retreveB=$_REQUEST['description'];
$retreveC=$_REQUEST['cost'];
?>
<table width="300" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>ID</td>
<td>Description</td>
<td>Cost</td>
</tr>
<tr>
<td><?PHP echo $retreveA; ?></td>
<td><?PHP echo $retreveB; ?></td>
<td><?PHP echo $retreveC; ?></td>
</tr>
<?PHP $no=1;
while ($URLarray[2] > $no){?>
<tr>
<td><?PHP echo $retreveA; ?></td>
<td><?PHP echo $retreveB; ?></td>
<td><?PHP echo $retreveC; ?></td>
</tr>
<?PHP $no++;
}?>
</table>
<?php $addition=$URLarray[2] + 1; ?>
<a href="quoteget.php?item=<?php echo "$URLarray[0]:$URLarray[1]:$addition:$URLarray[3]";?>">add</a>
<a href="quoteget.php?item=<?php echo "$URLarray[0]:$URLarray[1]:$addition:$URLarray[3]";?>">back</a>
I've made two pages, one shows information (a template) from the database in a form and allows users to edit it in a <textarea>. This then gets posted to this one where the information is displayed in a noneditable table. This works fine but...
I can to add an extra row to my table but the information recorded gets wiped/replaced when adding (I've looked at $_SESSION and cookies but its not helping?! EasyPHP gives me 'script possibly relies on a session side-effect').
Any help would be great thanks.
Last edited by LingoOutsider; May 26th, 2009 at 09:13 AM.
-
May 26th, 2009, 11:36 PM
#10
Re: generating multiple rows in a Table Problem
err. did I miss something? you're echoing the same thing over and over again ($retreveA -> $retreveC)? what for? what are $URLarray's 0, 1 and 3 keys used for? for whatever reason, I am having the hardest time trying to figure out what you're doing.
was there any reason you couldn't use arrays, like in my example?
-
May 27th, 2009, 12:11 AM
#11
Thread Starter
Addicted Member
Re: generating multiple rows in a Table Problem
I am using retreveA,B,C to capture 3 different fields/columns posted by the other page. I attempted to store information in an array but found that when I altered a value in an array it was changing other values?
so...
array(t1,t2,t3,t1,t2)=>try to change array[3] to t2 would make array(t3,t3,t2,t1,t2)??
I'll post my code shortly so you can have a look, I don't know why It was being orkward it all looked logical to me?
-
May 27th, 2009, 01:31 AM
#12
Thread Starter
Addicted Member
Re: generating multiple rows in a Table Problem
PHP Code:
<?php // connect to customer and work table error_reporting(0); $con = mysql_connect("server", "database", "password"); if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("database", $con); ?> <?PHP //URL array organised $lookatURL = $_GET["item"]; $URLarray = explode(":",$lookatURL); //still looking at URL $URLnumber = count($URLarray); print_r ($URLarray); ?>
<table width="100%" border="1" cellspacing="2" cellpadding="0"> <tr> <td width="10%" align="center" valign="top">Item</td> <td width="80%" align="center" valign="top"> Description</td> <td width="10%" align="center" valign="top">Total</td> </tr> <?PHP $boxRows = 0; while ($boxRows < $URLarray[1]){ $changeno = $boxRows + 2; ?><tr> <td width="10%"><form name="form1" id="form1"> <select name="item" onchange="MM_jumpMenu('parent',this,0)"> <option>Select</option><?PHP $trees = (mysql_query("SELECT * FROM treeworks ORDER BY ItemCode")); $q = mysql_query("SELECT * FROM treeworks WHERE id=$URLarray[2]"); while($row = mysql_fetch_array($trees)){ ?><option value="?item=<?PHP $URLarray[$changeno]="$row[id]"; // This is where it all goes wrong?? $makenote = 1; // URL repo all works! echo "$URLarray[0]"; while ($makenote < $URLnumber){ echo ":$URLarray[$makenote]"; $makenote++;} ?>"><?PHP echo "$row[ItemCode]"; ?></option> <?PHP } ?> </select> </form></td> <td width="80%"><?PHP while($row2 = mysql_fetch_array($q)){ echo $row2[DetailDescription]; print_r ($URLarray); } ?></td> <td width="10%"></td> </tr> <?PHP $boxRows++; } ?>
<tr> <td><a href="?item=<?PHP $boxNumber = ($URLarray[1]+1); $URLarray[1] = $boxNumber; $numbered = 0; while ($numbered < $URLnumber ) { echo "$URLarray[$numbered]:"; $numbered++; } ?>" class="style4">add</a></td> <td class="style2"><span class="style4"></span></td> <td></td> </tr> </table> <?PHP mysql_close($con); ?>
Not exactly the same as yours but I'm moving closer.
I have put the URL into an array and can loop the array back to the URL. But when I change a value in the array (ie [4]) it changes all the previous values in the array/URL to the last item on the database (ie [3][2][1][0] but [5][6]etc stay the same!?).
Last edited by LingoOutsider; May 27th, 2009 at 03:29 AM.
-
May 27th, 2009, 04:08 PM
#13
Re: generating multiple rows in a Table Problem
I don't see how when you were changing it using arrays that you could possibly be changing other values, unless you had something wrong! arrays seem like they would seriously take all of the guess work out of this, because if you want to change just key 4, you can just edit that key and be done with it completely. I know I'm not helping much with what you already have, but there seems to be a much easier way of doing this! the biggest problem with what you're doing is that everything needs to be in a certain order, and that makes things a little difficult. using an array like this, nothing needs to be in order. PHP can do that work for you.
I modified my script to work more like yours. it builds a URL instead of using a form, and I stole your JavaScript to make it submit as well.
PHP Code:
<script type="text/JavaScript"> <!-- function MM_jumpMenu(targ,selObj,restore){ //v3.0 eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); if (restore) selObj.selectedIndex=0; } //--> </script>
<?php //sort the keys so that they always look like they're in the correct order to the user //- even if they are in an incorrect order in the query string. ksort($_GET['items']); ?> <pre><?php print_r($_GET['items']); ?></pre> <hr />
<?php $array = array("Apple", "Orange", "Banana");
//loop through our array of items if(count($_GET['items'])){ foreach($_GET['items'] as $key => $value): ?> <?php //build the previous URL if(count($_GET['items'])){ $url = "?"; foreach($_GET['items'] as $rkey => $rvalue): if($rkey == $key) continue; $url .= "items[{$rkey}]={$rvalue}&"; endforeach; } ?> <select name="items[<?php echo $key; ?>]" onchange="MM_jumpMenu('parent',this,0)"> <option value="">Select</option> <?php foreach($array as $option): ?> <option value="<?php echo $url . "items[{$key}]={$option}"; ?>"<?php if($value == $option) echo ' selected="selected"'; ?>><?php echo $option; ?></option> <?php endforeach; ?> </select> <hr /> <?php endforeach; } ?> </xmp> </pre>
<?php /****************** ** adding items **/ ?>
<form action="quotes2.php" method="get"> <!-- previous items --> <?php if(count($_GET['items'])){ foreach($_GET['items'] as $key => $value): ?> <input type="hidden" name="items[<?php echo $key; ?>]" value="<?php echo htmlentities($value); ?>" /> <?php endforeach; } ?> <!-- /previous items --> <!-- new item --> <input type="hidden" name="items[<?php echo count($_GET['items']); ?>]" value="" />
<input type="submit" value="Add" /> </form>
if any of this code doesn't make sense to you, then let me know! dealing extensively with arrays can be daunting if you're not comfortable with them, but they're extremely useful. you can see it in action here. it should be easy to replace $array with an array of items from your database. you can do something simple, like this (this should be placed where $array is defined in my example):
PHP Code:
<?php $array = array(); $query = mysql_query("SELECT id, ItemCode FROM treeworks ORDER BY ItemCode"); while($tree = mysql_fetch_assoc($query)){ $array[$tree['id']] = $tree['ItemCode']; } ?>
combining these two examples, I think, should give you what you're looking for -- or close to it. of course, you will need to change the following lines to properly use the "new" $array:
PHP Code:
<?php foreach($array as $option => $display): ?> <option value="<?php echo $url . "items[{$key}]={$option}"; ?>"<?php if($value == $option) echo ' selected="selected"'; ?>><?php echo $display; ?></option> <?php endforeach; ?>
let me know how it goes!
-
May 28th, 2009, 01:56 AM
#14
Thread Starter
Addicted Member
Re: generating multiple rows in a Table Problem
Thanks for the code.
It uses code that I've seen but not realy used. I now want to show the description (from the database) of each tree selected next to the drop down box.
Should I put $_GET['items'] into a variable (It looks to be an array) and try to show each key in the order they are shown?
Will post my code if i get anywhere!
Thanks again.
-
May 28th, 2009, 03:20 AM
#15
Re: generating multiple rows in a Table Problem
no, you don't need to store $_GET['items'] in a variable; it's already a variable.
now, if the description is information that could also be stored in $array, then store it there to be called upon later. this seems like it would be the case. you can just add another key:
PHP Code:
<?php $array = array(); $query = mysql_query("SELECT id, ItemCode, description FROM treeworks ORDER BY ItemCode"); while($tree = mysql_fetch_assoc($query)){ $array[$tree['id']]['itemcode'] = $tree['ItemCode']; $array[$tree['id']]['description'] = $tree['description']; } ?>
then, you'll need to again update the way you handle displaying your <selects>.
PHP Code:
<?php foreach($array as $option => $display): ?> <option value="<?php echo $url . "items[{$key}]={$option}"; ?>"<?php if($value == $option) echo ' selected="selected"'; ?>><?php echo $display['itemcode']; ?></option> <?php endforeach; ?>
then, to display descriptions, you can do this as long as you're within the first foreach() loop:
PHP Code:
echo $array[$value]['description'];
hope that makes sense!
-
May 28th, 2009, 04:06 AM
#16
Thread Starter
Addicted Member
Re: generating multiple rows in a Table Problem
I've done it like this...
PHP Code:
//at the top! <?php $stoned=$_GET['items'];?>
PHP Code:
//after drop downbox. <?Php //show bref description $query = mysql_query("SELECT * FROM table WHERE Description='$stoned[$key]' ORDER BY ItemCode"); while($tree = mysql_fetch_assoc($query)){ echo "$tree[Description]";} ?>
It might not be as efficiant as yours but It works
Thanks for your help!
Last edited by LingoOutsider; May 28th, 2009 at 04:10 AM.
-
May 28th, 2009, 04:56 AM
#17
Thread Starter
Addicted Member
Forms and URLs??
If I want to record or pass a variable from one page to another i can do it by adding a ?x= command in the URL or send it via a form $_POST $_REQUEST etc can I do it any other way?
$_SESSIONS and $_COOKIES don't look to be working for me? I've looked at the examples at www.w3schools.com/PHP/ and I'm not sure they are used for this task?
-
May 28th, 2009, 02:27 PM
#18
Re: generating multiple rows in a Table Problem
if you want to pass variables across multiple pages in a short term situation, you should use sessions. if you want to pass them in long term situations, use cookies.
also, in regards to your previous post -- if your query's "WHERE" clause isn't a typo, you're asking for a description matching $stoned[$key] and then echoing that description. basically, that means you're echoing $stoned[$key]. just as well, there's no reason to define $stoned -at all- because to get the value of $stoned[$key] you can just use $value (defined in a foreach loop above).
the only other problem I could see with your code is that you're using a WHILE loop to display one record. you only need to use a loop with mysql_fetch_* functions when you're retrieving multiple rows. this works just as well:
PHP Code:
<?php $query = mysql_query("SELECT * FROM table"); $tree = mysql_fetch_assoc($query); echo $tree['Description']; ?>
and, finally, the last thing I'll say is that it's very inefficient to query the database for every single <select> on your form, especially if you can do one single query beforehand like in my example. it will take a lot of the server load off.
but, glad you got it working I suppose!
-
Jun 16th, 2009, 07:01 AM
#19
Thread Starter
Addicted Member
Re: [RESOLVED] generating multiple rows in a Table Problem
Sorry my mind has gone numb
I'm working with this code...
PHP Code:
<?php if(isset($_REQUEST[getitemsno])){ $getitemsno=$_REQUEST['getitemsno']; }else{ $getitemsno=count($_GET['items']);} //looking at items[] in the URL $getitems=$_GET['items']; if($_POST[Deluse]){ $getitemsno=$getitemsno-1; array_pop($getitems);}?> <input type="hidden" name="getitemsno" value="<?php echo "$getitemsno"; echo "$getitemsno";?>" />
There are multiple items[] in the URL but echo $getitemsno is showing nothing when I push my [Deluse] button (form is set to post), where have I gone wrong??!
-
Jun 16th, 2009, 04:14 PM
#20
Re: [RESOLVED] generating multiple rows in a Table Problem
well, if you didn't edit any of that code, then you should be getting some errors from the following lines:
PHP Code:
if(isset($_REQUEST[getitemsno])){
//and
if($_POST[Deluse]){
this is because getitemsno and Deluse are both strings, and PHP needs to know that. you need to refer to these as $_REQUEST['getitemsno'] and $_POST['Deluse'] when they're not being used inside of a string. as an aside, you're echoing $getitemsno twice inside of your <input>, so if you had it set as 3, it would be submitting as 33. not sure if that was intentional or not.
try not to be generic with using the $_REQUEST global unless you're actually using different types of request methods (post and get). if you aren't, be specific and use $_POST or $_GET. this will prevent your script from being used in a way it may not have been intended.
-
Jun 17th, 2009, 07:58 AM
#21
Thread Starter
Addicted Member
Re: [RESOLVED] generating multiple rows in a Table Problem
thanks kows you've been a great help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|