Having problems coding if and counter - php

I am trying to figure out how to do two things. One Query a database and get a record count (NO PROBLEM WITH THIS) Next I want to display a limited number of record on a webpage. this is where I get stuck. I am pretty sure I need to add a counter to the while loop but I keep having a problem only displaying one record 1000's of times.
if ($result->num_rows > 0) {
// output data of each row
echo "<center><table>";
echo "<tr>";
echo "<th>Legal Business Name</th>";
echo "<th>DBA Name</th>";
echo "<th>Business Address</th>";
echo "<th>Website Address</th>";
echo "<th>Business Government POC</th>";
echo "</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["LEGAL_BUSINESS_NAME"]. "</td>";
echo "<td>" . $row["DBA_NAME"]. "</td>";
echo "<td>" . $row["PHYSICAL_ADDRESS_LINE_1"]. "<br>" . $row["PHYSICAL_ADDRESS_LINE_2"]. "<br>" . $row["PHYSICAL_ADDRESS_CITY"]. ", " . $row["PHYSICAL_ADDRESS_PROVINCE_OR_STATE"]. " " . $row["PHYSICAL_ADDRESS_ZIP_POSTAL_CODE"]. "+" . $row["PHYSICAL_ADDRESS_ZIP_CODE_PLUS_4"]. " " . $row["PHYSICAL_ADDRESS_COUNTRY_CODE"]. "</td>";
echo "<td>" . $row["CORPORATE_URL"]. "</td>";
echo "<td>" . $row["GOVT_BUS_POC_FIRST_NAME"]. " " . $row["GOVT_BUS_POC_MIDDLE_INITIAL"]. " " . $row["GOVT_BUS_POC_LAST_NAME"]. "<br> P: ". $row["GOVT_BUS_POC_US_PHONE"]. "<br> E: ". $row["GOVT_BUS_POC_EMAIL"]. "</td>";
echo "</tr>";
}
echo "</table>";

Ok I figured this out. Sometimes you need a bigger hammer!
I added in a counter and created an if statement right after the while loop.
$int = 0;
while($row = $result->fetch_assoc()) {
if ($int < 10) {
echo "<tr><td>" . $row["LEGAL_BUSINESS_NAME"]. "</td>";
echo "<td>" . $row["DBA_NAME"]. "</td>";
echo "<td>" . $row["PHYSICAL_ADDRESS_LINE_1"]. "<br>" . $row["PHYSICAL_ADDRESS_LINE_2"]. "<br>" . $row["PHYSICAL_ADDRESS_CITY"]. ", " . $row["PHYSICAL_ADDRESS_PROVINCE_OR_STATE"]. " " . $row["PHYSICAL_ADDRESS_ZIP_POSTAL_CODE"]. "+" . $row["PHYSICAL_ADDRESS_ZIP_CODE_PLUS_4"]. " " . $row["PHYSICAL_ADDRESS_COUNTRY_CODE"]. "</td>";
echo "<td>" . $row["CORPORATE_URL"]. "</td>";
echo "<td>" . $row["GOVT_BUS_POC_FIRST_NAME"]. " " . $row["GOVT_BUS_POC_MIDDLE_INITIAL"]. " " . $row["GOVT_BUS_POC_LAST_NAME"]. "<br> P: ". $row["GOVT_BUS_POC_US_PHONE"]. "<br> E: ". $row["GOVT_BUS_POC_EMAIL"]. "</td>";
echo "</tr>";
$int = $int + 1;
}
}

Related

Printing alphabets from a to z every time

I am using the below php code to list Alphabetical(A-Z) characters in the rows. It works fine from a to z. My problem is that after "z", it starts with aa, ab,ac, etc.. I'd like it to start with a to z again every time it comes to z.
How can achieve this? thanks.
<?php
foreach (range('a', 'z') as $char)
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
echo "<td>" .$char. "</td>";
$char++;
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
?>
I tried below solution but it ended up with the same result.
<?php
$char= 'a';
for($i=0;$i<26;$i++)
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
echo "<td>" .$char. "</td>";
$char++;
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
?>
You could utilize PHP's built-in array functions for handling internal array pointers:
$range = range('a', 'z');
while ($row = mysqli_fetch_array($search_result)) {
echo "<tr>";
echo "<td>" . current($range) . "</td>";
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
if (next($range) !== false) {
reset($range);
}
}
Checkout these references to learn more:
http://php.net/manual/en/function.current.php
http://php.net/manual/en/function.next.php
http://php.net/manual/en/function.reset.php
You can reference your array of letters by a calculated value, $i % 26, where $i is the zero based iteration count of your while loop.
$letters = range('a', 'z');
$i = 0;
while ($row = mysqli_fetch_array($search_result)) {
echo "<tr>";
echo "<td>" . $letters[$i++ % 26] . "</td>";
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
Firstly, get the characters from a to z in an array.
Initialize a counter (index) to access a specific character from the array.
Increment the counter inside loop.
If the counter value goes above 25 (index starts from 0 - so this represents 26th array item), reset its value back to zero.
Try the following:
<?php
// get array for characters from a to z
$characters = range('a', 'z');
$counter = 0; // initialize the counter (index)
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
// access the character using counter index
echo "<td>" . $characters[$counter] . "</td>";
// Increment the counter
$counter++;
// Check and reset if the counter has gone above 25 (26th array item)
$counter = ($counter > 25 ? 0 : $counter);
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
?>

mysqli query. Is there any way to display error? even though my query works fine

I am trying to display some data from the database into an html table. My other query works fine, i was able to retrieve some of the other tables and displaying it to my website although this particular query does not even though if have a function mysqli_error(). I tried executing the query directly from my database (phpmyadmin) and it retrieve it just fine.
here are my codes.
Note: $p_id variable has some data which is '3'.
PHP
$p_sql = "SELECT p.package_name, p.package_price, p.package_details, p.package_categories, p.package_id FROM event_table as e inner join package as p on e.package_id = p.package_id where e.package_id = '$p_id' group by e.package_id";
$data_p = mysqli_query($conn, $p_sql) ;
$result = mysqli_fetch_assoc($data_p);
$p_amount = $result['package_price'];
while ($record_p = mysqli_fetch_array($data_p)) {
echo "<table border = 1>";
echo "<tr>";
echo "<th>" . "Package Name" . "</th>";
echo "<th>" . "Package Price" . "</th>";
echo "<th>" . "Package Details" . "</th>";
echo "<th>" . "Package Categories" . "</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . "<br />" . $record_p['package_name'] . "<br />" . "</td>" ;
echo "<td>" . "<br />" . $record_p['package_price'] . "<br />" . "</td>" ;
echo "<td>" . "<br />" . $record_p['package_details'] . "<br />" . "</td>" ;
echo "<td>" . "<br />" . $record_p['package_categories'] . "<br />" . "</td>" ;
echo "<tr/>";
}
echo "</table>";
if(!$data_p){
echo("Error description: " . mysqli_error($conn));
}

rowCount based on row value

So I am trying to do this..
$userID = $_SESSION['user_session'];
$stmt = $this->db->prepare("SELECT * FROM orrs");
$stmt->bindparam(":id", $userID);
$stmt->execute();
$count = $stmt->rowCount();
echo
"<div class='table-responsive'>
<table class='table' border='1'>
<tr class='head'>
<h3>Snapshot</h3>
<th>Se</th>
<th>#s</th>
<th>Ae</th>
<th>Prt</th>
<th>Pin</th>
</tr>";
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}
Basically, If the value in the row STAGE = 1 I want it to count those rows and give me the number.. If the value of STAGE = 2 I want it to count those rows and give me the number.
Right now, It is just counting all of the rows.. So for both of the IF statment its count number is saying 2, When there is only 1 in each section..
I think you need to execute three different statements, one to get all the rows (for you to loop over and create your output) and one to get each of the counts
//The current one
$stmt = $this->db->prepare("SELECT * FROM orrs");
//The get the count for stage '1'
$stage_1_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 1");
$stage_1_count = $stage_1_stmt->rowCount();
//The get the count for stage '2'
$stage_2_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 2");
$stage_2_count = $stage_2_stmt->rowCount();
You can execute these others to get the counts which you should use in place of $count in your loop.
Your while loop then becomes
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $stage_1_count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $stage_2_count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}

mysqli_fetch loop not working

I have a loop inside other loop which is not working, this is the code:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
When I query $result2 directly into the BBDD for the first result it shows three results but the code doesn't go in the second LOOP. Why? Any error here?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$result2 = mysqli_query($connection, $result2);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
Use:
$query = "SELECT ....";
$result2 = mysqli_query($db, $query);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
Before read this How can I prevent SQL injection in PHP? topic. After try to use mysql_query()
Try This
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$results = mysqli_query($db,$result2);
while($row2 = mysqli_fetch_array($results))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row['outcomeName'] . "</td>";
}
echo "</tr>";
}
?>
Before fetching data execute mysql query using mysqli_query() function then run mysqli_fetch_array(). It would be good if you count result as $count = mysqli_num_rows($query) and manage your code with if... else .
I think $result2 should be output of mysqli_query not just merely query. Talking in analogous to MySQL.
Probably you should have something like this
$result2 = mysqli_query($result2);

insert hyperlink in php mysql query

I am trying to display a link in a php document. the data is stored in mysql. I have stored the url in the field course_url.
i can get the page to show the hyperlink asd a plain text but want it to show ashyperlink with "Click Here" anchor text. The coding i got so far is:
<?php
$con=mysqli_connect("localhost","root","","mentertraining");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT `coursedates`.`coursedate_id`,`coursedates`.`course_id`,`coursedates`.`date1`,`courses`.`course_title`,`courses`.`course_url`,`courses`.`no_of_days` FROM coursedates\n"
. "LEFT JOIN `mentertraining`.`courses` ON `coursedates`.`course_id` = `courses`.`course_id` LIMIT 0, 30 ";
$result = mysqli_query($con,$query);
echo "<table border='1'><tr><th>Course Title</th><th>Course Date</th><th>No of Days</th><th>Course URL</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
$date = new DateTime($row['date1']);
$row['date1'] = $date->format('d/m/Y');
echo "<tr>";
echo "<td>" . $row['course_title'] . "</td>";
echo "<td>" . $row['date1'] . "</td>";
echo "<td>" . $row['no_of_days'] . "</td>";
echo "<td>""<a href=" . $row['course_url'] . >"'Click Her'"</a>""</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Your echo is malformed on this line:
echo "<td>""<a href=" . $row['course_url'] . >"'Click Her'"</a>""</td>";
It should be:
echo "<td><a href='" . $row['course_url'] . "'>Click Here</a></td>";
You have used the wrong "' quotations
while($row = mysqli_fetch_assoc($result))
{
$date = new DateTime($row['date1']);
$row['date1'] = $date->format('d/m/Y');
echo "<tr>";
echo "<td>" . $row['course_title'] . "</td>";
echo "<td>" . $row['date1'] . "</td>";
echo "<td>" . $row['no_of_days'] . "</td>";
echo "<td><a href='" . $row['course_url'] ."'>Click Her</a></td>";
echo "</tr>";
}

Categories