Alternative to using PHP-GMP to populate two column table - php

A page is using the PHP GNU Multiple Precision (GMP) to determine how many rows should end up in each column. While the code works, we would like to migrate the application to a new host that does not support the PHP5-GMP module. That said, what might be the best alternative to generating and populating a two column table that may not always have an even number of results? Further the table design is probably not the best so I am open to alternatives.
The code below taken from here gets us close but since it uses mysql_fetch_array, it places the items like:
a b
c d
instead of
a c
b d
Is there a way to have it display in the first example?
Newer code:
<head>
<style>
.container { width: 400px; float: left; }
.container .item { width: 50%; float: left; height: someFixedHeight; }
</style>
</head>
<?php
mysql_select_db("database",$db);
$cat= $_GET["cat"];
echo '<div class="container">';
$q = mysql_query("SELECT * FROM name WHERE Field4 = '$cat'",$db);
while ($res = mysql_fetch_array($q)){
echo '<div class="item">' . $res['Field1'] . '</div>';
}
echo '</div>';
?>
Original code:
<div id="bit-list" align="center" >
<table class="list" width="600" border="0" align="center">
<tr>
<td width="300" height="72" valign="top" border="1">
<div align="left">
<?php
$result = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
$myrow3 = mysql_fetch_row($result);
$num_rows = mysql_num_rows($result);
$d3 = gmp_div_q("$num_rows", "2", GMP_ROUND_PLUSINF);
$i = 1;
$result8 = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
while ($myrow3 = mysql_fetch_row($result8))
{
if ($i <= gmp_strval($d3))
{
echo "<p>";
echo '<a href="detail.php?';
echo 'page=';
echo "$myrow3[2]";
echo '&';
echo 'pnum=';
echo "$myrow3[6]";
echo '">';
echo "$myrow3[1]";
echo "</p>";
$i = $i + 1;
}
else
{
}
}
?>
</div></td>
<td width="300" valign="top" border="1">
<div align="left">
<?php
$result = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
$myrow3 = mysql_fetch_row($result);
$num_rows = mysql_num_rows($result);
$d4 = gmp_div_q("$num_rows", "2", GMP_ROUND_MINUSINF);
$d3 = gmp_div_q("$num_rows", "2", GMP_ROUND_PLUSINF);
$j = 1;
$result18 = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
while ($myrow3 = mysql_fetch_row($result18))
{
if ($j <= gmp_strval($d3))
{
$j=$j+1;
}
else
{
echo "<p>";
echo '<a href="detail.php?';
echo 'page=';
echo "$myrow3[2]";
echo '&';
echo 'pnum=';
echo "$myrow3[6]";
echo '">';
echo "$myrow3[1]";
echo "</p>";
$j = $j + 1;
}
}
?>
</div>
</td>
</tr>
</table>
</div>

If i read well, you use gmp_div_q for rounding only? Then, you should check round().
The only case, that round() cannot cover is GMP_ROUND_ZERO, but you don't use that (and you could cover it with a simple if).

Related

Paginate custom SQL query

I'm really new to WordPress backend. Here I'm trying to pass variables through $_POST and get results according to those variables from my database. Everything is working fine, the only problem I'm facing is that I'm getting 80 to 90 results and I want to paginate this thing.
I've tried doing it by bypassing the page variable in the URL, but I think $_POST variables are not passing on page 2.
I'm adding the code below.
<?php
$graphic = $_POST["graphics"];
$mem = $_POST["memo"];
$opsys = $_POST["osys"];
$cpuu = $_POST["cpu"];
$hddd = $_POST["hdd"];
?>
<?php
$rowcount = $wpdb->get_var("SELECT COUNT(*) FROM game_list WHERE game_ram <= $mem AND game_card <= $graphic AND game_hdd <= $hddd AND game_os <= $opsys AND game_cpu <= $cpuu ");
?>
<p>
<?php
global $wpdb;
global $row;
$result = $wpdb->get_results( "SELECT * FROM game_list where game_ram <= $mem AND game_card <= $graphic AND game_hdd <= $hddd AND game_os <= $opsys AND game_cpu <= $cpuu order by release_date desc");
?>
<div>
<div style="padding:7px;"><h3> We found <font style="color:blue;"><?php echo $rowcount; ?></font> Results</h3><br>Reset Parameters/Search Again</div>
<?php
foreach ($result as $row) {
echo "<div class=\"column\" style=\"width:100%;min-height: fit-content;padding:5px; margin:3px;border: 1px solid;border-radius: 15px; float: left;\">
<div class=\"column\" style=\"width:25%; float: left;\">
<img src=\"";
echo $row->game_icon;
echo "\" style=\"height: 138px;width:98px;\">
</div>
<div class=\"column\" style=\"padding:5px;width:75%; float: left;\"><a href=\"";
echo $row->game_link;
echo "\"><b>";
echo $row->game_name;
echo "</a></b><hr> <b>WGod7 Game Score : </b>";
echo $row->metacritic_score;
echo "<br><b>Release Date : </b>";
echo $row->release_date;
echo "<span style=\"float:right;\">Read More...</span>";
echo "</div></div>";
}
?>
</div>

How add font within Echo php in old script?

Please I have an old code, and I need format it with google font or other fonts,
I was try some statement but not work,
Please help!
-------------- The Code --------------
<?php header("Content-type: text/html; charset=utf-8");?>
<?php
include "inc/config.inc.php";
?>
<table width="100%" dir="rtl">
<?php
$query = "SELECT person_id, name, DATE_FORMAT(updated, ".$datefmt.") AS ddate, suffix FROM ".$tblprefix."people";
if ($_SESSION["id"] == 0)
$query .= " WHERE date_of_birth < '".$restrictdate."'";
$query .= " ORDER BY updated DESC LIMIT 0,5";
$result = mysql_query($query) or die($err_changed);
$i = 0;
while ($row = mysql_fetch_array($result)) {
if ($i == 0 || fmod($i, 2) == 0)
$class = "tbl_odd";
else
$class = "tbl_even";
?>
<tr>
<td class="<?php echo $class; ?>" align="right"><li><a target="_blank" href="people.php?person=<?php echo '<style = "font-color: #ff0000"> $row["person_id"]; ?>"><?php echo $row["name"]." ".$row["suffix"]; ?></a></td>
<!-- <td class="<?php echo $class; ?>"><?php echo $row["ddate"]; ?></td> -->
</tr>
<?php
$i++;
}
mysql_free_result($result);
?>
</table>
If you just want to use alternate styling for odd and even rows, you can do that with plain CSS using the :odd or :even selectors.

how to get multiple values of drop down from a loop?

<?php
// this is where selecting of number of rooms in database
$sql = mysqli_query($con, 'SELECT * FROM roomtype');
$get_room1 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=2");
$get_room2 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=3");
$get_room3 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=4");
$get_room4 = mysqli_query ($con, "SELECT * from room where status='Activated' and type_id=5");
$standard = mysqli_num_rows($get_room1);
$deluxe = mysqli_num_rows($get_room2);
$family = mysqli_num_rows($get_room3);
$regular = mysqli_num_rows($get_room4);
// this is the loop for the drop down and it will display the number of rooms available
while($row = mysqli_fetch_array($sql))
{
$a=$row['type_id'];
$b = $row['rmtype'];
$_SESSION['room_type'] = $row['rmtype'];
echo '<div style="height: 300px;">';
echo '<div style="float: left; width: 100px; margin-top: 15px; margin-left: 60px;">';
echo "<img width=250 height=200 alt='Unable to View' src='" . $row["image"] . "'>";
echo '</div>';
echo '<div class="well" style="float: right; width: 780px; margin-top: 5px;">';
echo '<div style="float: right;">';
echo '</div>';
echo '<br />';
echo "<label style='margin-left: px;'>Number of rooms:";
echo "<select name='select_no_rooms[]' value='" .$row['rmtype']." ' />";
echo "<option> </option>";
if ($row['rmtype']=="Standard Room")
{
for ($x = 1; $x<=$standard; $x++){
echo "<option name='standard'>$x</option>";}
}
if ($row['rmtype']=="Deluxe Room")
{
for ($x = 1; $x<=$deluxe; $x++){
echo "<option name='deluxe'>$x</option>";}
}
if ($row['rmtype']=="Family Room")
{
for ($x = 1; $x<=$family; $x++){
echo "<option name='family'>$x</option>";}
}
if ($row['rmtype']=="Regular Room")
{
for ($x = 1; $x<=$regular; $x++){
echo "<option name='regular'>$x</option>";}
}
echo "</select>";
}
?>
This is the design of drop down,
add [ ] tag to name tag, so you will get your value in array.
<option name='deluxe[]'>$x</option>
First thing that I notice is that in you select statement, you cannot have value as an attribute. Please check the documentation select tag
And the option tag does not include name attribute. Please check the documentation option tag
If you want to get multiple values from a drop down list. Please check this article

Activating certain PHP code before a section of the page is Created

I am working with an HTML webpage connected to a database and using PHP. I have in a div at the top a variable that doesn't get properly set until some PHP later in the document. Is there a way to make the div build after the later PHP, but still be where it should be at the top of the page?
Here's the code that uses the variables
<div id="HeaderRight">
<div id="HeaderRight">
<?php
echo '<a href="../Cart/index.php" class="header">
<img src="../Images/shoppingcart.gif" height="18" width="127" border="0" align="ABSMIDDLE" /> Contains '.$_SESSION["num_items"].' Items</a>'
?>
</div>
Here's the code that sets the variables
<?php
echo '<TABLE WIDTH="100%" BGCOLOR="#FFFFFF" ALIGN = "RIGHT" BORDER = "1">';
echo '<TR BGCOLOR = "818996" HEIGHT = "25"><TD>Product</TD><TD>Availability</TD> <TD>Qty</TD><TD>Price</TD><TD>Total</TD></TR>';
$strTotalPrice = 0;
for ($i=0; $i<$_SESSION['num_products']; $i++) {
$strSQL = "SELECT * FROM products WHERE ProductCode = '".$_SESSION['cart'][$i]['name']."'";
$rsProd = mysql_query($strSQL)
or die($db_name . " : " . $strSQL . " : " . mysql_error());
$rowProd = mysql_fetch_array($rsProd);
$strSQLDetails = "SELECT * FROM pagedetails WHERE Category = '".$rowProd["Category"]."'";
$rsDetails = mysql_query($strSQLDetails)
or die($db_name . " : " . $strSQLDetails . " : " . mysql_error());
$rowDetails = mysql_fetch_array($rsDetails);
// End Of "Retrieve Desired Record Set"
echo ' <TR HEIGHT = "' .$rowProd["ThumbnailHeight"]. '" ><TD style="vertical-align:middle"><img src="../'.$rowProd["Department"].'/Images/'.$rowProd["ProductCode"].'_sm.jpg" align = "left" border="0" height = "'.$rowProd["ThumbnailHeight"].'" width="100" hspace="25" />
<br><a href = "../Products/Products.php?PageID='.$rowDetails["PageID"].'" >'.$rowProd["ProductName"].'</a>
</TD>';
$strAvailability = "";
$strQuantity = 0;
if($rowProd["Stock"] < $_SESSION["cart"][$i]["qty"]){
$strAvailability = "Only " .$rowProd["Stock"]. " in Stock, Quantity Changed";
$strQuantity = $rowProd["Stock"];
$_SESSION["cart"][$i]["qty"] = $rowProd["Stock"];
}
else{
$strAvailability = "In Stock";
$strQuantity = $_SESSION["cart"][$i]["qty"];
}
echo '<TD ALIGN = "center">'.$strAvailability.'</TD>';
echo '<TD ALIGN = "center">
<input type="text" name="txtQuantity" size="2" maxlength="2" value ='.$strQuantity.'>';
echo '<form action="index.php?=?product='.$rowProd["ProductCode"].'&quantity=1" method="post">
<input type="submit" name="submit" value="Update">
</form>
</TD>';
$strPrice = $rowProd["RegPrice"] * $strQuantity;
$strTotalPrice += $strPrice;
echo '<TD ALIGN = "center">$'.$rowProd["RegPrice"].'.00</TD>';
echo '<TD ALIGN = "center">$'.$strPrice.'.00</TD></TR>';
//echo '( <a href=index.php?product=' . $_SESSION['cart'][$i]['name'] . '&quantity=1>+</a> )';
}
As Rocket Hazmat points out, it is a good idea to run all of your PHP code before rendering the HTML. Without rewriting your current code, you can achieve this with some output buffering:
<?
// add this block
function firstDiv() {
ob_start();
?>
<div id="HeaderRight">
... <? echo $_SESSION["num_items"]; ?> ...
</div>
<?
// ... and this block
$_SESSION["firstDiv"] = ob_get_contents();
ob_end_clean();
} // end of firstDiv
?>
and the same with the rest of your page
<?
function restOfYourPage() {
ob_start();
?>
<TABLE> ... <? $_SESSION["num_items"] = 666; ?> ... </TABLE>
<?
$_SESSION["restOfYourPage"] = ob_get_contents();
ob_end_clean();
} // end of restOfYourPage
?>
Then you can run the php code in reverse order, but display it in normal order:
<?
restOfYourPage();
firstDiv();
echo $_SESSION["firstDiv"];
echo $_SESSION["restOfYourPage"];
?>

PHP MYSQL, How to show records in four columns ,

Here is my code the records shows in four columns but if my records is blank it shows three balng images, any suggestions?
$query = mysql_query("SELECT * from rbf_events_images where event_id='".$_GET['id']."'");
echo '<table border="1">';
if(count(mysql_num_rows($query)>0)):
$tropentags='<tr>';
$troclosingtags='</tr>';
$formTags="";
$tdTags="";
$count=1;
while($row = mysql_fetch_array($query)){
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
if ($count>3)
{
$formTags.=$tropentags.$tdTags.$troclosingtags;
$tdTags="";
$count=0;
}
$count=$count+1;
}
if ($count>0)
{
for($i = 1; $i <= (4-$count) ; $i++)
{
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
}
$formTags.=$tropentags.$tdTags.$troclosingtags;
}
echo $formTags;
endif;
Thanks for your help!really appreciated!
I noticed that on lines like this one:
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
You are delimiting the string with single quotes ('), and you are also trying to embed a variable in the string that uses single quotes. I'm not sure how you did not get compile errors for that. I would switch to:
$tdTags= '<td align="left" valign="middle" class="td">' . $row['image'] . '</td>';
Here's what I usually do to put records in columns:
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * from rbf_events_images where event_id='$id'");
echo '<table border="1"><tbody><tr>';
if (mysql_num_rows($query) > 0) {
$count = 0;
while ($row = mysql_fetch_array($query)) {
if ($count && $count % 4 == 0) echo '</tr><tr>';
echo '<td align="left" valign="middle" class="td">'.$row['image'].'</td>';
$count++;
}
}
echo '</tr></tbody></table>';

Categories