Jump to content

[SOLVED] Multiple Coding Questions


KarmicStigmata

Recommended Posts

I'm still a beginner at coding with PHP, as this would be Day 3 of what I have learned.  I've looked multiple places but have not been able to find what I want.

 

Question 1:  Is it possible to link a MYSQL Query to a php Variable, where it would be able to render different results?

Example:
$var1 = $row['mysql1'];
$var2 = $row['mysql2];
$txt1 = 'Accepted'

if $var1 ==< 1.0 && $var2 ==< 4
  echo $txt1;

 

Question 2:  Is it possible to create an automated menu using this code?

 

$rowsperpage=10;
$result = mysql_query("SELECT * DB
WHERE Name='blank'
ORDER BY Age DESC LIMIT " .($rowsperpage*(is_numeric($_GET['page'])?($_GET['page']-1):0)).", ".($rowsperpage*(is_numeric($_GET['page'])?$_GET['page']:1)).";");

As you can see it is based off the rows per page, I stole it from a guest book.  The guest book requires that it uses an ID number; however, the code itself creates an ordered list and I would need that list to continue over a series of pages that would be automatically created by a script.  If it doesn't make sense I can go into further detail.

 

Link to comment
Share on other sites

Question 1:  Is it possible to link a MYSQL Query to a php Variable, where it would be able to render different results?

not entirle sure what you mean here but if i understand correctly you want to use a mysql result within php. if so the answers yes. mysql_fetch_array/mysql_fetch_assoc will enable you to get mysql resulting data and use the results within php (see here and here)

 

Question 2:  Is it possible to create an automated menu using this code?

Not with that code alone you cant.. you would need to create an actual output from the results, the code you have is just the backbone (actually getting db data) see the basic pagination tut (link below)

 

A few tutorials which may help

basic database handling

basic pagination

 

If i've totally missed the point apologies :D

 

Stuie

Link to comment
Share on other sites

Right now I'm not worried about the variable thing I'm worried about the pagination.  It isn't displaying the first line from the db and I'm using CV's.. reading through the comments apparently I'm not the only one that has had an issue with this.

 

// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM DB";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 0) * $rowsperpage;

// get the info from the db 
$sql = "SELECT * FROM DB
ORDER BY Experience DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
   // echo data
   echo "<div align='center'><table><tr><td>$txt1</font><br><font size='2'>$txt2</font><hr></td></tr><tr><td><table border='0' align='center' width='800' cellpadding='0' cellspacing='0''>
<tr>
<th><center>IGN<center></th>
<th><center>Rank<center></th>
<th><center><b><font color='#EEB502'>Total Experience</font color></b><center></th>
<th><center>KdR<center></th>
<th><center>Clan<center></th>
<th><center>Emblem<center></th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><center>" . $row['IGN']. "<center></td>";
  echo "<td><center><img src='" . $row['Ranks']. "'><center></td>";
  echo "<td><center>" . $row['Experience']. "<center></td>";
  echo "<td><center>" . $row['KDR'] . "<center></td>";
  echo "<td><center>" . $row['Clan']. "<center></td>";
  echo "<td><center><img src='" . $row['Clan']. "' width='21' height='21'><center></td>";
  echo "</tr>";
  }

$table = "</table></div>";
echo $table;
} // end while

 

I don't know where I am going wrong in the code.  His was set for exactly what I needed, PRECISELY what I needed, and entirely wrong if people can't see the top person in a ranking system! :(  The links have also been killed in the code.

 

Here's a link to the actual page so you can see what I mean: Display Problem .

 

Also, Stuie thank you for the pagination information!

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.