How can I echo the first row of a while loop using mysql_fetch_assoc?
help please?PHP Code:while($row2 = mysql_fetch_assoc($result2)) {
| SitePoint Sponsor |
How can I echo the first row of a while loop using mysql_fetch_assoc?
help please?PHP Code:while($row2 = mysql_fetch_assoc($result2)) {


If you only want the first row, then don't use a loop.
PHP Code:$row2 = mysql_fetch_assoc($result2);
echo $row2['some_column_name'];
17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more.
excuse me I didn't explain myself that well.
The while loop below uses mysql_fetch_assoc and it has several rows and it is located in a file call example1.php. But what I want is to call the first row of this while loop found in example1.php from another file call shoe.php. it's that possible?
PHP Code:$sql = 'SELECT id,Subject FROM menusubjects;';
$result = mysql_query($sql);
if($result && mysql_num_rows($result)!=0) {
echo '<ul id="nav-categories">';
while($row = mysql_fetch_assoc($result)) {
$uri = 'example1.php?subject='.urlencode($row['id']);
$class = !is_null($cat) && $cat==$row['id']?' class="selected"':'';
echo "\t",'<li',$class,'><a href="',$uri,'">',$row['Subject'].'</a>';
if($submenu==false && !is_null($cat) && $cat == $row['id']) {


How are example1.php and shoe.php related? Is shoe.php included somewhere after all this code?
Two options:
1) Put the first row in a variable which will be in scope wherever shoe.php gets included
2) If $result2 is still in scope where shoe.php gets included, then use mysql_data_seek() to rewind the row pointer to the beginning and call mysql_fetch_assoc() to get the first row again
17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more.
Example1.php
PHP Code:$sql = 'SELECT id,Subject FROM menusubjects;';
$result = mysql_query($sql);
if($result && mysql_num_rows($result)!=0) {
echo '<ul id="nav-categories">';
while($row = mysql_fetch_assoc($result)) {
$uri = 'example1.php?subject='.urlencode($row['id']);
$class = !is_null($cat) && $cat==$row['id']?' class="selected"':'';
echo "\t",'<li',$class,'><a href="',$uri,'">',$row['Subject'].'</a>';
if($submenu==false && !is_null($cat) && $cat == $row['id']) { // line 58
$sql2 = 'SELECT id,shoename FROM regularmenu WHERE menusubject_id = '.mysql_real_escape_string($cat).';';
$result2 = mysql_query($sql2);
if($result2 && mysql_num_rows($result2)!=0) {
echo "\n\t\t",'<ul class="submenu">',"\n";
while($row2 = mysql_fetch_assoc($result2)) {
$uri2 = $uri.'&menu='.urlencode($row2['id']);
$class2 = !is_null($prod) && $prod==$row2['id']?' class="selected"':'';
echo "\t\t\t",'<li',$class,'><a href="',$uri2,'">',$row2['shoename'],'</a></li>',"\n";
}
echo "\t\t",'</ul> <!-- end of ul.submenu -->',"\n";
}
$submenu = true;
}
echo '</li>',"\n";
}
echo '</ul> <!-- end of ul#nav-categories -->',"\n
$query = "SELECT shoe.shoename, shoe.price, shoe.moreinfo
FROM shoe
WHERE shoe_kind = ". (int) $_GET['shoe']; // $_GET value I extract it from the (isset) selection at the top of the code. This condition will show the content if the shoe_kind is == to $_GET value. I don't know if the query or purpose described above need a query to be place before this query or after this query and table found on this final part of my code.
echo $query
$result = mysql_query($query, $connection);
while ($content = mysql_fetch_array($result)) {
echo "<table style=\"float:left\">
<td width=\"150\" style=\"text-align:center;\">" . $row['shoename'] . "</td>
<tr>
<td height=\"100\" width=\"100\" style=\"position:relative;\">
<img src=\"../images/shoesname.jpg\" alt=\"sd\" width=\"97\" height=\"80\" border=\"1\" style=\"border-color:#FF6600;\" />
</td></tr>
<tr>
<td width=\"5\" height=\"21\" ></td><td>" . $row['price'] . "</td>
</tr><td>" . $row['moreinfo'] . "</td>
</table>";
}
?>
Shoe.php
The only difference in between both files are the last queries and I want that last query of shoe.php to open together with the first query in shoe.php or example1.php. If you notice both the first and the last query call the same field 'Subject' FROM menusubjects in both files. I want that when a user click on a subject in the last query of shoe.php then the first raw of the mysql_fetch_assoc in the first query which call the field subjects as well opens just too. (the first query will open subheadings through query number 2 in example1.php or shoe.php "$sql2".PHP Code:
$sql = 'SELECT id,Subject FROM menusubjects;';
$result = mysql_query($sql);
if($result && mysql_num_rows($result)!=0) {
echo '<ul id="nav-categories">';
while($row = mysql_fetch_assoc($result)) {
$uri = 'example1.php?subject='.urlencode($row['id']);
$class = !is_null($cat) && $cat==$row['id']?' class="selected"':'';
echo "\t",'<li',$class,'><a href="',$uri,'">',$row['Subject'].'</a>';
if($submenu==false && !is_null($cat) && $cat == $row['id']) { // line 58
$sql2 = 'SELECT id,shoename FROM regularmenu WHERE menusubject_id = '.mysql_real_escape_string($cat).';';
$result2 = mysql_query($sql2);
if($result2 && mysql_num_rows($result2)!=0) {
echo "\n\t\t",'<ul class="submenu">',"\n";
while($row2 = mysql_fetch_assoc($result2)) {
$uri2 = $uri.'&menu='.urlencode($row2['id']);
$class2 = !is_null($prod) && $prod==$row2['id']?' class="selected"':'';
echo "\t\t\t",'<li',$class,'><a href="',$uri2,'">',$row2['shoename'],'</a></li>',"\n";
}
echo "\t\t",'</ul> <!-- end of ul.submenu -->',"\n";
}
$submenu = true;
}
echo '</li>',"\n";
}
echo '</ul> <!-- end of ul#nav-categories -->',"\n
$sql = "SELECT menusubjects.Subject, menusubjects.image
FROM menusubjects";
$result = mysql_query($sql, $connection);
if(!$result){
die("Database query failed:" . mysql_error());}
while ($content = mysql_fetch_array($result)) {
echo "<table width=\"260\" height=\"100\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#FF0033\" bgcolor=\"#FFFFFF\" style=\"float:left; position:relative; left:206px; top:
300px; height:5px;\">
<td width=\"95%\ height=\"10%\"> <table width=\"96%\" height=\"10%\"> <td style=\"width:100%\"><img src=\"../images/lineforheader.gif\" alt=\"gf\" width=\"100%\" /></td><tr><td bordercolor=\"#666666\" bgcolor=\"#D83607\" height=\"100%\" style=\" width:140px;\"><a href=\"#\">
<div class=\"shoe2\" align=\"left\" style=\"background-image:url(../images/titleshoe.gif);\">" . $content['Subject'] . "</div>
</a></td></tr>
<tr>
<td style=\"width:100%; background:#CC0000; height:2px;\"><a href=\"#\">" . $content['image'] . "</a></td>
</tr></table></td>
</table></td>";
}
PS: I was wondering if I could do all this within one file since they contain the same code with some differences. Let's say shoe.php would be the file to choose instead of trying to relate both files shoe.php and example1.php.
Thank you.


That doesn't answer anything. You have two files. Are they the code behind two completely separate requests from the browser? PHP is stateless, one request can't access data from the other, so if you get from example.php to shoe.php by clicking a link, whatever data you need to relate them needs to be part of the link; if by a form, then part of the form, etc.
17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more.
I am relating them through links. I include a link within shoe.php which calls the example1.php file and that's how i connect them each other. That would be a separate request from the browser?
that's your option number 2.
If $result2 is still in scope where shoe.php gets included, then use mysql_data_seek() to rewind the row pointer to the beginning and call mysql_fetch_assoc() to get the first row again
Since the process would starts when the users click on the field subject on shoe.php found in the link to Subjects in the last query of the shoe.php, I have wonder what do I use to make the pointer rewind using mysql_data_seek () to only open the first row of the mysql_fetch_assoc() in the first query of shoe.php and open it together just by clicking on the Subject field in the last query of the shoe.php.
forget about example1.php I think it would be irrelevant to use at this point since I have realize that most of the code found in example1.php is found in shoe.php as well and there is no need to go back to example1.php to use a code found in shoe.php.


You can't use option #1 or #2 as these are separate requests. You have to pass whatever info you need through the link.
17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more.
Cool I got that Never thought I could use the link to do that. Learners...
This the link.
What I want to affect from that link is the followingPHP Code:<a href=\"#\">
<div class=\"shoe2\" align=\"left\" style=\"background-image:url(../images/titleshoe.gif);\">" . $content['Subject'] . "</div>
</a
I want that if the subject id of the last query == to subject id in the first query then go to the while loop.PHP Code:
$submenu = false;
$cat = isset($_GET['subject']) && is_numeric($_GET['subject'])?$_GET['subject']:null;
$prod = isset($_GET['menu']) && is_numeric($_GET['menu'])?$_GET['menu']:null;
$sql = 'SELECT id,Subject FROM menusubjects;';
$result = mysql_query($sql);
if($result && mysql_num_rows($result)!=0) {
echo '<ul id="nav-categories">';
while($row = mysql_fetch_assoc($result)) {
$uri = 'example1.php?subject='.urlencode($row['id']);
$class = !is_null($cat) && $cat==$row['id']?' class="selected"':'';
echo "\t",'<li',$class,'><a href="',$uri,'">',$row['Subject'].'</a>';
if($submenu==false && !is_null($cat) && $cat == $row['id']) { // line 58
both query call the same table and field so that won't be a problem.
That would be:
Not Idea how to do next..PHP Code:<a href=\"<?php if($cat["id"] == subject {\">
<div class=\"shoe2\" align=\"left\" style=\"background-image:url(../images/titleshoe.gif);\">" . $content['Subject'] . "</div>
</a>
help please


I'd like to help, really. But I can never make sense of what you're asking to do so. I'm unsubscribing from the thread, sorry. Hope someone else knows what's up.
17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more.
Bookmarks