Jump to content

syntax problem, help with the correct way to display


CodeMama

Recommended Posts

I have this query and snippet which works and I can echo the correct records:

 $query =  "SELECT admin.UserName, admin.AdminID, workorders.WorkOrderID,
workorders.CreatedDate, workorders.Location, workorders.WorkOrderName,
workorders.FormName, workorders.STATUS, workorders.Notes, workorders.pod
FROM admin LEFT JOIN workorders ON (admin.AdminID = workorders.AdminID)
WHERE admin.Retail1 = 'yes'
";
  
    $result = mysql_query ($query) ;    
    //$row = mysql_fetch_array($result);
    while ($row = mysql_fetch_row($result)){
    $admin_id = $row['AdminID'];
    for ($i=0; $i<mysql_num_fields($result); $i++);
        echo $row[$i] . " ";
        
        }

 

My problem lies in now I want to take the hodge podge display of the echo$row[$i] and neatly display it and I am trying to use something like this:

<?php echo $row['WorkOrderID']; ?>

but get nothing I also tried with just the $i ... what is the correct way to use the results from the query to display individual field results?

Link to comment
Share on other sites

mysql_fetch_row - Get a result row as an enumerated array

mysql_fetch_array - Fetch a result row as an associative array, a numeric array, or both

mysql_fetch_assoc - Fetch a result row as an associative array

 

...so you need to use one of the last two

 

 

Link to comment
Share on other sites

I tried that originally using assoc and array but then I was only getting the first record in the table and not all of them, the way it is now it returns all of them so I am wondering how to take

$row['$i'] and turn that back into an array?

 

$display = mysql_fetch_assoc($row['$i'])  ??? something along these lines. but I'm still having the syntax issue i'm guessing because I get the blank page of death...

Link to comment
Share on other sites

Well no blank page but my $display variable returns nothing so I'm losing my results somewhere:

    $query =  "SELECT admin.UserName, admin.AdminID, workorders.WorkOrderID,
workorders.CreatedDate, workorders.Location, workorders.WorkOrderName,
workorders.FormName, workorders.STATUS, workorders.Notes, workorders.pod
FROM admin LEFT JOIN workorders ON (admin.AdminID = workorders.AdminID)
WHERE admin.Retail1 = 'yes'
";
  
    $result = mysql_query ($query) ;    
    //$row = mysql_fetch_array($result);
    while ($row = mysql_fetch_row($result)){
    $admin_id = $row['AdminID'];
    for ($i=0; $i<mysql_num_fields($result); $i++);
        //echo $row[$i] . " ";
        $display = mysql_fetch_assoc($row['$i']) . " "; not sure about this ...         echo $display['$i'] . " ";   
        }
        

Link to comment
Share on other sites

no...like this:

 

 $query =  "SELECT admin.UserName, admin.AdminID, workorders.WorkOrderID,
workorders.CreatedDate, workorders.Location, workorders.WorkOrderName,
workorders.FormName, workorders.STATUS, workorders.Notes, workorders.pod
FROM admin LEFT JOIN workorders ON (admin.AdminID = workorders.AdminID)
WHERE admin.Retail1 = 'yes'
";

    $result = mysql_query ($query) ;   
    while ($row = mysql_fetch_array($result)){
      $admin_id = $row['AdminID'];
      print_r($row);
    }

Link to comment
Share on other sites

Well that def. works and pulls the correct records but then when I try to display them neatly like this:

 

<TR bgcolor="<?php echo $row_color ?>">
                <TD><FONT SIZE="-2">K<?php echo $row['WorkOrderID']; ?></FONT></TD>
                <TD><FONT SIZE="-2"><?php echo $row['Location']; ?></FONT></TD>
                <TD><FONT SIZE="-2">

 

I get nothing for WorkOrderID or Location....

Link to comment
Share on other sites

Here is the code below what's working and I have highlighted where it stops echoing the results:

 

//Query works to return correct records 
    $query =  "SELECT admin.UserName, admin.AdminID, workorders.WorkOrderID,
workorders.CreatedDate, workorders.Location, workorders.WorkOrderName,
workorders.FormName, workorders.STATUS, workorders.Notes, workorders.pod
FROM admin LEFT JOIN workorders ON (admin.AdminID = workorders.AdminID)
WHERE admin.Retail1 = 'yes'
";

    $result = mysql_query ($query) ;   
    while ($row = mysql_fetch_array($result)){
      $admin_id = $row['AdminID'];
      //print_r($row);
      echo '<pre>' . print_r($row,true) . '</pre>';
    }

   
        
    if ($row['Retail1'] == "NO") {
        header ("Location: Welcome.php?AdminID=$AdminID&msg=Sorry, you do not have access to that page.");
    }
    
if (isset($_GET['SortBy'])) {$SortBy = $_GET['SortBy'];} else {$SortBy = 'WorkOrderID DESC';}
if (isset($_GET['Page'])) {$Page = $_GET['Page'];} else {$Page = 1;}

$PerPage = 30;
$StartPage = ($Page - 1) * $PerPage;

        
     
     
      $Total = ceil(mysql_num_rows($result)/$PerPage);
    
     //Reveal Variables for Debugging
   // include("VariableReveal2.php");
If ($Page > 0) {$PagePrev = ($Page - 1);} else {$PagePrev = '';}
If ($Page < $Total) {$PageNext = ($Page + 1);} else {$PageNext = '';}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Work Order System - Administrative Section</TITLE>
<LINK REL="STYLESHEET" HREF="inc/style.css">
</head>
<body>

<TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0">
<tr>
<td colspan="9">
this will not display here... even though it does above in the code... 
<?php    while ($row = mysql_fetch_array($result)){      $admin_id = $row['AdminID'];      echo '<pre>' . print_r($row,true) . '</pre>';}?>
</td>
</tr>

------------------------------------------------------------

Link to comment
Share on other sites

So there is no way to just echo the records like echo $row['fieldname'] (the regular way) because of the WHILE?  Is it possible to move the closing loop bracket to encompass the html on the page for the neatly table layout of the results?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.