tags:

views:

327

answers:

4

Hello,

I have the following code, which works fine:

echo '<div id="Result">Content goes here</div>';
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo '<td><a href="#" onclick="GetAuctionData()">' . $cell . '</a></td>';

    echo "</tr>\n";
}

Result gets filled with the result of the ajax query, which calls this.

echo "<table border='1' width='100%'>
<tr>
<th>test1</th>
<th>test2</th>
<th>test3</th>
</tr>";

$sql="select * from auctions where ARTICLE_NO='110294394238';
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
    echo "<tr>
    <td>".$row['SELLER_ID']."</td>
    <td>".$row['ACCESSSTARTS']."</td>
    <td>".$row['ARTICLE_NAME']."</td>
    </tr>";
}
echo "</table>";

My questions are these:

How can I make the query in the second block of code, match the row being clicked on from the first block of code. i.e. not have article_no hardcoded in, but replaced with the article_no of what I click on. Would a variable have to be shared between both files somehow?

Is it difficult to change the table layout to layers, or it would not matter?

One of the fields contains a link to an image, is there a way to embed that somehow? Something like <img src=".$row['PICTURE']."> ?

edit:

function GetAuctionData()
{
xmlHttp=GetXmlHttpObject()
if(xmlHttp==null)
{
alert("Your browser is not supported?")
}
var url="get_auction.php?"
url=url+"cmd=GetAuctionData"
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange = FetchComplete;
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
A: 

Can you pass it as an argument to the GetAuctionData() function?

dylanfm
no, GetAuctionData is just the xmlHttp stuff
Joshxtothe4
A: 

If you were using mysqli, I'd suggest using mysqli_prepare and mysqli_stmt_bind_param...

R. Bemrose
+2  A: 

In order to accomplish the first thing that you are proposing, you will need to pass the article number through GetAuctionData function in a manner like this:

echo '<td><a href="#" onclick="GetAuctionData('. $article_no . ')">' . $cell . '</a></td>';

Then in whatever you are using to handle the Ajax request, you will need to pass the variable to the PHP script as either a GET or POST variable. If you're using a javascript framework to do your Ajax handling, you can consult their documentation for information about how to pass variables.

In regard to your third question, what you are proposing is fine, you just have to make certain that you are pointing to valid file system path with the src attribute.

Noah Goodrich
I am not using a framework and cannot convert to one. I have posted the function GetAuctionData in, and am not sure if it can be modified in the way you describe?
Joshxtothe4
It has to be. PHP takes incoming variables via GET and POST. You have to be able to pass the selected article_no is some manner.
Noah Goodrich
Additionally, I would recommend that you look at prototype. You can begin by just rewriting your GetAuctionData function to use the prototype Ajax.Request or Ajax.Updater object to handle your ajax request.
Noah Goodrich
+1  A: 

To modify your function, I would recommend that you first switch to a Javascript framework / library that provides ajax handling. These libraries are more robust and are really easier to use than hard coding it all yourself.

Then, I would change your function to look more or less like the following (I used prototype for my example but any library would work fine):

function GetAuctionData(article_no) {
    new Ajax.Updater('div_to_update', 'get_auction.php', {
      method: 'get',
      parameters: {cmd: 'GetAuctionData', sid: Math.random(), article: article_no}
     }
}

Now, my previous suggestion will work beautifully to pass your query value into the second script.

Please keep in mind that this really is how the rest of the world handles ajax requests and its expected that variables have to be passed through GET, POST, or SESSION for manipulation between scripts.

Noah Goodrich