extract value from array


#1

OK, here is my php code so far…I’m lrearning so be gentle!!!

[php]
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
print “

n”;
foreach ($line as $col_value) {
if ($col_value == “”){
print " n";
}
else {
print “$col_valuen”;
}
}
print "<td CLASS=FS NOWRAP><a href='future_page.php?id='$line[1]>Purchase</a></td>n";
print "</tr>n";

}
[/php]

The table populates correctly based on the query and database values.

My problem is the a href tag, where I want to load a new page based on the id number of the record (the 1st field in the array). I know it’s simple, but I cant figure it out…

Any help is appreciated


#2

alright, got it…

[php]
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo “

n”;
foreach ($line as $col_value) {
if ($col_value == “”){
echo " n";
}
else {
echo “$col_valuen”;
}
}
echo “<a href=page.php?id=”, $line[‘ID’], “>Purchasen”;
echo “n”;
}
[/php]

It’s not pretty, but it works…

Is there a better way to clean this up?


#3

I can see four things that don’t comply to the ‘best-practice’ way of doing things:

  • Tag names and attributes are always lowercase in HTML
  • Your tag attribute values need quotes around them
  • String concatenation in PHP is done through a period, not a comma
  • It’s best to keep PHP variables outside quotes

[php]
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo “

n”;
foreach ($line as $col_value) {
if ($col_value == “”) {
echo " n";
}
else {
echo “”.$col_value.“n”;
}
}
echo “<a href='page.php?id=”.$line[‘ID’]."’>Purchasen";
echo “n”;
}
[/php]

#4

Thanks forthe quick tutorial!!!

I’ll have more questions later on this project…are you able to help with session IDs??


#5

No more than the average brick :slight_smile:
I store ‘sessions’ in a sessions table in MySQL, and the session ID in a cookie on the client’s computer (with some defty IP/userid/salt/hash checking to more or less guarantee session hijacking is out of the question of course).


#6

Not sure if I should start a new thread or continue with the current one???

Anyway, as I stated, I might need more help…

Based on the short code I posted, you may have noticed that if a user clicks on a perticular record, a new page will open. This page list only the record being updated. The table has several fields, but the important ones are ID, Locked…

The original page is set to display only records that are not locked (locked=0). When a user selects a record, the new page opens, sets Locked=1 and displays the rest of the fields for that ONE record. The user can then add their information into the feilds, click the submit button and the record is updated…simple. I have also added a button that cancels the update. When the user clicks the cancel button, the update query sets Locked=0 making it available again on the main page.

My problem now is if the user simply closes the window, without either entering their data or clicking cancel, the locked record never gets released… I need to trap the window closing and update the record to locked=0…

Do you follow me?? I was told that session IDs could be used, but I havn’t got an idea how to!!


#7

I would advise against ‘trapping’ the user in the website. I’m not sure if that’s even possible, but it’s highly annoying to have somethin like that happen to you. I guess you could use the following construction:

When the user opens a record, the record gets locked and a ‘lock date’ gets set. Whenever the user visits another page of your website, the lock date gets updated. When the user doesn’t visit a page for an extended period, say, 15 minutes, you could ‘expire’ the lock and just unlock the record automatically.

Alternatively, you could also have the lock remain in place and whenever the user returns to the website, notify them that there’s still X number of records locked by them.


#8

Sorry, when I said TRAP I meant to say detect the event of closing the window and updating the record to locked=0

Right now user opens the record and the field Locked is set to equal “1”.

What I want to do is if the user closes the browser window, the field Locked gets updated to equal ‘0’.

Yes, it would be OK if I can get it to do that within 10-15 minutes, but how?? The user has closed the browser window, and has presumeably, left my website…


#9

You know how to work with include(). Just use a global file that checks the Locked state and lock date of all your records, and unlocks any records that are locked and have expired the lock date timeout.


#10

Thanks Zyppora for the great idea…I did get it all working with one little issue left with a mysql query…

this is the Select:

[php]
$result = mysql_query("SELECT ID,description,cost FROM $sqltbl WHERE Locked = ‘0’ OR Sold = ‘0’ ");
[/php]

When I run the querry, I still get all the rows returned, including the ones where Locked = 0 or Sold= 0 …

| ID  | Description   | Cost | Locked    |  Sold  |
-------------------------------------------------------
| 1   |  Item 1       | 10   | 0         |   1    |
| 2   |  Item 2       | 20   | 1         |   0    |
| 3   |  Item 3       | 15   | 0         |   0    |
| 4   |  Item 4       | 10   | 1         |   1    |

If I run the querry as is, everything is returned…
If I remove the WHERE sold=‘0’ Then it filters out all the Locked = 1 (as expected)
If I remove WHERE Locked=‘0’ it filters out all the Sold= 1 as expected…

I need the querry to remove all rows where either sold = 1 or locked = 1 or any combination of the two (as long as one of them is = 1 [or both] then filter it out)


#11
WHERE Locked = '0' OR Sold = '0' ");

Take a look at what that OR does, and consider what any other logical operator would do :wink: Operators such as AND for example.