Jump to content

creating link from variable


hyster

Recommended Posts

ive done a page that works fine but id like to add links to the results so i can view the models.

working page here http://sts.hostei.com/dsgi/list_records.php

in the make column i want a link that will only show the make clicked.

 

if this was a fixed page i no how to do it but i cannot figure out to make it from the way ive discribed.

 

i know i have to alter this line

<td><div align="center"><?php echo $rows['make']; php?></div></td>

but no idea how to do this.

do i create a new page for the query? if so how do i pass the $var?

is it possable to do this from the same page? ie appear undr the main table.

 

Thanks

 

<?php
include("config.php"); 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT make, COUNT(*) AS total, SUM(IF(comments = \'pass\', 1, 0)) AS withComments FROM dsgi_serval GROUP BY make ORDER BY COUNT(*) DESC"; 
$result=mysql_query($sql);
php?>

<table width="319" border="1"><tr>
      <td colspan="4"><font size="4" face="Verdana">
         <strong><div align="center">Validation Total's </div></strong></font> </td>
   </tr>
    <tr>
       <td width="137" align="center"><strong>Make</strong></td>
       <td width="85" align="center"><strong>Total</strong></td>
       <td width="75" align="center"><strong>Validated</strong></td>
    </tr>
<?php
while($rows=mysql_fetch_array($result)){
php?>
    <tr>
        <td><div align="center"><?php echo $rows['make']; php?></div></td>
        <td><div align="center"><?php echo $rows['total']; php?></div></td>
        <td><div align="center"><?php echo $rows['withComments']; php?></div></td>
     </tr>
<?php
}
php?>
     <tr>

         <td><div align="center">Totals</div></td>
         <td> <div align="center">
         
           <?php
// counts all rows
           $query = "SELECT make, COUNT(make) FROM dsgi_serval "; 
           $result1 = mysql_query($query) or die(mysql_error());

           // Print out result
           while($row1 = mysql_fetch_array($result1)){
           echo $row1['COUNT(make)'];
}
?>
        </div></td>
        <td><div align="center">
       
<?php

// counts passes
           $query = "SELECT make, COUNT(make) FROM dsgi_serval where comments like 'pass'"; 
           $result2 = mysql_query($query) or die(mysql_error());
           while($row2 = mysql_fetch_array($result2)){
           echo $row2['COUNT(make)'];

}
?>
       </div></td>
      </tr>
</table>



<?php
mysql_close();
php?>

Link to comment
https://forums.phpfreaks.com/topic/219289-creating-link-from-variable/
Share on other sites

so you want a page that displays only the make that was clicked right?

 

if so, you can use $_GET to pass the make that was clicked,

so that when http://sts.hostei.com/dsgi/list_records.php?make=hp

is loaded, run this sql:

 

"SELECT make, COUNT(*) AS total, SUM(IF(comments = \'pass\', 1, 0)) AS withComments FROM dsgi_serval WHERE make = '. $_GET['make'] ."GROUP BY make ORDER BY COUNT(*) DESC"

 

this is just a simple example, of course you want to use something like htmlspecialchars on the $_GET variable before you run your sql

im stuck here now

 

url = http://sts.hostei.com/dsgi/test.php?make=HP

 

$result5 = mysql_query("SELECT * FROM dsgi_serval where make like '$get["make"]'") 
or die(mysql_error());  

im getting

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in 
/home/a3288237/public_html/dsgi/test.php on line 76

 

full code

<?php
include("config.php"); 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT make, COUNT(*) AS total, SUM(IF(comments = 'pass', 1, 0)) AS withComments FROM dsgi_serval GROUP BY make ORDER BY COUNT(*) 
DESC"; 
$result=mysql_query($sql);
php?>

<table width="319" border="1"><tr>
      <td colspan="4"><font size="4" face="Verdana">
         <strong><div align="center">Validation Total's </div></strong></font> </td>
   </tr>
    <tr>
       <td width="137" align="center"><strong>Make</strong></td>
       <td width="85" align="center"><strong>Total</strong></td>
       <td width="75" align="center"><strong>Validated</strong></td>
       <td width="75" align="center"><strong>Not Validated</strong></td>
    </tr>
<?php
while($rows=mysql_fetch_array($result)){
php?>
    <tr>
        <td><div align="center"><a href="?make=<?php echo $rows['make']; php?>"><?php echo $rows['make']; php?></a></div></td>
        <td><div align="center"><?php echo $rows['total']; php?></div></td>
        <td><div align="center"><?php echo $rows['withComments']; php?></div></td>
        <td><?php 
               
                 echo $rows['total']-$rows['withcomments'];
                 
                 ?>



     </tr>
<?php
}
php?>
     <tr>

         <td><div align="center">Totals</div></td>
         <td> <div align="center">
         
           <?php
// counts all rows
           $query = "SELECT make, COUNT(make) FROM dsgi_serval "; 
           $result1 = mysql_query($query) or die(mysql_error());

           
           while($row1 = mysql_fetch_array($result1)){
           echo $row1['COUNT(make)'];
}
?>
        </div></td>
        <td><div align="center">
       
<?php

// counts passes
           $query = "SELECT make, COUNT(make) FROM dsgi_serval where comments like 'pass'"; 
           $result2 = mysql_query($query) or die(mysql_error());
           while($row2 = mysql_fetch_array($result2)){
           echo $row2['COUNT(make)'];

}
?>
       </div></td>
       <td></td>
      </tr>
</table>

<?php

$result5 = mysql_query("SELECT * FROM dsgi_serval where make like '$get["make"]'") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while($row5 = mysql_fetch_array( $result5 )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row5['make'];
echo "</td><td>"; 
echo $row5['model'];
echo "</td></tr>"; 
} 

echo "</table>";

?>



<?php
//mysql_close();
php?>


$get is not the same as $_GET. Change that line to:

$result5 = mysql_query("SELECT * FROM dsgi_serval where make like '{$_GET['make']}'")

 

Also change all the php?> close tags to simply ?>

big thanks guys, it works fine.

 

hopefully not to push my luck (and didnt no if to start a new thread with this)

 

this returns the $rows['total'] var and dosent do the sum but if i manualy add numbers it works ok.

echo $rows['total']-$rows['withcomments'];

Make sure they both have valid values. If $rows['total'] has a value, but $rows['withcomment'] does not, it will appear to just echo the value of $rows['total'] because the value won't change.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.