bubble sort without for each php - php

hi i cant seem to get this to work and i keep getting undefined error in line 8 and 11. Here is my code
<?php
$count = 0;
$temp = 0;
$name = array("Suzuki", "Holden", "Jaguar", "Toyota", "Hyundai", "Ford", "Honda", "Mazda");
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . "<p>";
for ($incount = 0; $incount <= 7; $incount++) {
if ($name[$incount] > $name[$incount + 1]) {
$temp = $name[$incount];
$name[$incount] = $name[$incount + 1];
$name[$incount + 1] = $temp;
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . $count++ . "<p>";
}
}
?>

here is mistake:
$name[$incount] > $name[$incount + 1]
when $incount = 7 then $incount +1 = 8 what is undefined for you... because you defined just 7 elements...
solution can be just $incount < 7

You are not using the correct algorithm for bubble sort. It needs two loops.
<?php
$count = 0;
$temp = 0;
$name = array("Suzuki", "Holden", "Jaguar", "Toyota", "Hyundai", "Ford", "Honda", "Mazda");
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . "<p>";
for ($incount = 0; $incount <= 7; $incount++) {
for ($innercount = $incount+1; $innercount <= 7; $innercount++) {
if ($name[$incount] > $name[$innercount]) {
$temp = $name[$incount];
$name[$incount] = $name[$innercount];
$name[$innercount] = $temp;
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . $count++ . "<p>";
}
}
}
?>

<?php
$count = 0;
$temp = 0;
$name = array("Suzuki", "Holden", "Jaguar", "Toyota", "Hyundai", "Ford", "Honda", "Mazda");
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . "<p>";
for ($incount = 0; $incount < 8; $incount++) {
if(isset($name[$incount + 1]))
if ($name[$incount] > $name[$incount + 1]) {
$temp = $name[$incount];
$name[$incount] = $name[$incount + 1];
$name[$incount + 1] = $temp;}
echo $name[0] . " " . $name[1] . " " . $name[2] . " " . $name[3] . " " . $name[4] . " " . $name[5] . " " . $name[6] . " " . $name[7] . $count++ . "<p>";
}
?>
But
Better Use
<?php
$count = 0;
$temp = 0;
$name = array("Suzuki", "Holden", "Jaguar", "Toyota", "Hyundai", "Ford", "Honda", "Mazda");
foreach($name as $f) echo $f." ";
echo "<p>";
for ($incount = 0; $incount < 8; $incount++) {
if(isset($name[$incount + 1]))
if ($name[$incount] > $name[$incount + 1]) {
$temp = $name[$incount];
$name[$incount] = $name[$incount + 1];
$name[$incount + 1] = $temp;}
foreach($name as $f) echo $f." ";
echo $count++ . "<p>";
}
?>

Related

wpdb->insert producing duplicates

I am trying to make a button on a page that prints out data from the database and then you can press 2 different buttons, one that deletes them from the database and the other one inserts it into another table in the database and deletes the data from the database, but it keeps inserting it twice into the new table and I have no clue why, this here prints out the data and session variables + buttons:
if(!isset($_POST['orderby'])) {
foreach ($requests as $row) {
echo "<div class='requests'>" . "<li class='refunds'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
} else {
foreach ($pergele as $row) {
echo "<div class='requests'>" . "<li class='refunds2'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Hylkää</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Hyväksy</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
}
and this should insert it into the database once and delete the data from the old table:
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$custname = $_SESSION['custname'];
$prodname = $_SESSION['prodname'];
/* Query to do whatever here */
$wpdb->insert("wp_acceptedrequests", [
"customer_name" => "$custname",
"name_product" => "$prodname",
"date" => date("Y/m/d/G:i:sa") ,
]);
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
What makes them insert twice and how do I prevent it from doing that?
I just ran into a similar situation where $wpdb inserts where being duplicated.
In my case it was happening if I was authenticated and browser inspector was open.

Add multiple if statements in a single php CI fucntion

Nerd question, apologies, Below function works when there is only single if statement but when i add multiple if statements, it won't work, as far i know about php currently i used elseif statement but also id didn't worked
function NewNoticeMSG($insert_id) {
$notice = $this->CI->notification_model->get_notice($insert_id);
$stu_setting = $this->CI->setting_model->getSkoolInfo();
if ($notice['visible_student'] == 'Yes') {
$students = $this->CI->student_model->get_all_students();
foreach($students as $student) {
$MSG = "Dear Student, NOTICE " . $notice['date'] . ": " . $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
$content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
'&message=' . rawurlencode($MSG) .
'&senderId=' . rawurlencode($this->senderId) .
'&routeId=' . rawurlencode($this->routeId) .
'&mobileNos=' . rawurlencode($student['mobileno']) .
'&smsContentType=' . rawurlencode($this->smsContentType);
$smsglobal_response = $this->sendSMS($content);
}
}
if ($notice['visible_parent'] == 'Yes') {
$students = $this->CI->student_model->get_all_students();
foreach($students as $student) {
$MSG = "Dear Parent, NOTICE " . $notice['date'] . ": " .
$notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
$content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
'&message=' . rawurlencode($MSG) .
'&senderId=' . rawurlencode($this->senderId) .
'&routeId=' . rawurlencode($this->routeId) .
'&mobileNos=' . rawurlencode($student['guardian_phone']) .
'&smsContentType=' . rawurlencode($this->smsContentType);
$smsglobal_response = $this->sendSMS($content);
}
}
if ($notice['visible_teacher'] == 'Yes') {
$teachers = $this->CI->teacher_model->get_all_teachers();
foreach($teachers as $teacher) {
$MSG = "Dear Teacher, NOTICE " . $notice['date'] . ": " . $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
$content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
'&message=' . rawurlencode($MSG) .
'&senderId=' . rawurlencode($this->senderId) .
'&routeId=' . rawurlencode($this->routeId) .
'&mobileNos=' . rawurlencode($teacher['phone']) .
'&smsContentType=' . rawurlencode($this->smsContentType);
$smsglobal_response = $this->sendSMS($content);
}
}
}
how should i do that??
Firstly you need to on error reporting then you should print array what you get then use isset and !empty in if statement,
For example:-
if (isset($notice['visible_student']) &&
!empty($notice['visible_student']) && $notice['visible_student'] ==
'Yes')
instead of this, "if ($notice['visible_student'] == 'Yes')"
Hope it will help you!
Please Try with the :
function NewNoticeMSG($insert_id) {
$notice = $this->CI->notification_model->get_notice($insert_id);
$stu_setting = $this->CI->setting_model->getSkoolInfo();
if (isset($notice['visible_student']) && $notice['visible_student'] == 'Yes') {
$students = $this->CI->student_model->get_all_students();
foreach($students as $student) {
$MSG = "Dear Student, NOTICE " . $notice['date'] . ": " . $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
$content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
'&message=' . rawurlencode($MSG) .
'&senderId=' . rawurlencode($this->senderId) .
'&routeId=' . rawurlencode($this->routeId) .
'&mobileNos=' . rawurlencode($student['mobileno']) .
'&smsContentType=' . rawurlencode($this->smsContentType);
$smsglobal_response = $this->sendSMS($content);
}
}
if (isset($notice['visible_parent']) && $notice['visible_parent'] == 'Yes') {
$students = $this->CI->student_model->get_all_students();
foreach($students as $student) {
$MSG = "Dear Parent, NOTICE " . $notice['date'] . ": " .
$notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
$content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
'&message=' . rawurlencode($MSG) .
'&senderId=' . rawurlencode($this->senderId) .
'&routeId=' . rawurlencode($this->routeId) .
'&mobileNos=' . rawurlencode($student['guardian_phone']) .
'&smsContentType=' . rawurlencode($this->smsContentType);
$smsglobal_response = $this->sendSMS($content);
}
}
if (isset($notice['visible_teacher']) && $notice['visible_teacher'] == 'Yes') {
$teachers = $this->CI->teacher_model->get_all_teachers();
foreach($teachers as $teacher) {
$MSG = "Dear Teacher, NOTICE " . $notice['date'] . ": " . $notice['title'] . " . Thank You, " . $stu_setting['name'] . ".";
$content = 'AUTH_KEY=' . rawurlencode($this->AUTH_KEY) .
'&message=' . rawurlencode($MSG) .
'&senderId=' . rawurlencode($this->senderId) .
'&routeId=' . rawurlencode($this->routeId) .
'&mobileNos=' . rawurlencode($teacher['phone']) .
'&smsContentType=' . rawurlencode($this->smsContentType);
$smsglobal_response = $this->sendSMS($content);
}
}
}

PHP numbering by unique ID grouping

I want to auto numbering but by user group
Query :
$query = "select distinct(DATE_FORMAT(a.in_time, '%Y%m%d')) as in_time, a.user_id, a.notes, b.nama from attendance a left join dat_login b on(a.user_id=b.ID) where DATE_FORMAT(a.in_time, '%Y')='2017' and DATE_FORMAT(a.in_time, '%M')='April' and id_shop!=2 order by b.nama asc";
$querynm = "select distinct(b.nama) as nama from attendance a left join dat_login b on(a.user_id=b.ID) where DATE_FORMAT(a.in_time, '%Y')='2017' and DATE_FORMAT(a.in_time, '%M')='April' group by b.nama order by b.nama asc";
$stid = mysqli_query($conn, $query) or die (mysqli_error($conn));
$stnm = mysqli_query($conn, $querynm) or die (mysqli_error($conn));
Result :
$alphabet_start = 'A';
while ($data = mysqli_fetch_assoc($stid)) {
$data['user_id'] += 1;
$no_cell = 11;
echo $data['nama'] . " ";
echo $data['user_id'] . " ";
if ($data['notes']=="") {
echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Masuk <br>";
} else if ($data['notes']=="S") {
echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Sakit <br>";
} else if ($data['notes']=="L") {
echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Libur <br>";
}
$alphabet_start++;
$no_cell++;
$data['user_id']++;
}
But, the value showing like this :
I want the value showing like this :
As you can see the number is changing in the next name, how to do it ?
Sorry for my bad grammar. Thanks before :)
Your code is a bit confusing but I think I understand what you mean.
You can do it as such:
$alphabet_start = 'A';
$userID = "";
$cells = array();
$cellNum = 11;
while ($data = mysqli_fetch_assoc($stid)) {
$data['user_id'] += 1;
if (in_array($data['user_id'], $cells)) {
$no_cell = $cellNum;
} else {
$no_cell = ($cellNum += 1);
$cells[] = $data['user_id'];
}
echo $data['nama'] . " ";
echo $data['user_id'] . " ";
if ($data['notes'] == "") {
echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Masuk <br>";
} else if ($data['notes'] == "S") {
echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Sakit <br>";
} else if ($data['notes'] == "L") {
echo chr(ord("A") + date('d', strtotime($data['in_time']))) . $no_cell . " - " . chr(ord("A") + date('d', strtotime($data['in_time']))) . ". " . "Libur <br>";
}
$alphabet_start++;
$no_cell++;
$data['user_id']++;
}

Multiple table echos not displaying in page

The above code is designed to display info stored in sql table. everything is corresponding to the titles in the table and in the correct order. however the page it is from is only displaying the first 2 columns and not the others. everything looks as if it is in order to me. is my statement wrong?
<?php
$con=mysqli_connect("xxx","y","y","yyyy");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tripdata ");
while($row = mysqli_fetch_array($result))
{
echo $row['trip_id'] . " " . $row['image'] . " " . $row['date'] . " " . $row['destination'] . " " . $row['hl'] . " " . $row['cost'] . " " . $row['blurb'] . " " . $row['whatinc'] . " " . $row['whatopt'] . " " . $row['itin'] . " " . $row['depinfo'] . " " . $row['ppcode'];
echo "<br>";
}
mysqli_close($con);
?>
maybe you can try using assoc
while($row = mysqli_fetch_assoc($result))
{
echo $row['trip_id'] . " " . $row['image'] . " " . $row['date'] . " " . $row['destination'] . " " . $row['hl'] . " " . $row['cost'] . " " . $row['blurb'] . " " . $row['whatinc'] . " " . $row['whatopt'] . " " . $row['itin'] . " " . $row['depinfo'] . " " . $row['ppcode'];
echo "<br>";
}
i usual use this and no problem
assoc is index name based on field name
but array is based on number (0, 1, 2, 3)
Strange. Try do loop instead:
do {
echo $row['trip_id'] . " " . $row['image'] . " " . $row['date'] . " " . $row['destination'] . " " . $row['hl'] . " " . $row['cost'] . " " . $row['blurb'] . " " . $row['whatinc'] . " " . $row['whatopt'] . " " . $row['itin'] . " " . $row['depinfo'] . " " . $row['ppcode'];
echo "<br>";
} while($row = mysqli_fetch_array($result));

Create text file out of query results

I need to create a text file from a foreach loop (code below) but i'm not sure how. So far I have tried to use fwrite but I'm not to sure how to write that much data to a txt file. I also tried to use file_put_content(), it worked but it displayed all of the HTML and had no line breaks. Any ideas on how to do display it?
Foreach loop
echo "<p>" . 'custom_weapons_v3' . "</p>";
echo "<p>" . '{' . "</p>";
foreach($arr as $key => $a){
echo "<br>";
$check = $a['steamid'];
echo $check;
echo "<br>";
echo "{";
echo "<br>";
foreach($data as $key => $r){
$check2 = $r['steam'];
if($check == $check2){
echo " \"{$r['wepid']}\" //{$r['wepname']}<br />";
echo '{' . "<br />";
echo '"level"' . ' "' . $r['weplvl'] . '"' . "<br />";
echo '"quality"' . ' ' . '"' . $r['weprare'] . '"' . "<br />";
if($r['attr1'] === 0 || $r['attr1'] === ''){
echo '';
}else{
echo '"1"' . ' "' . $r['attr1'] . ' ; ' . $r['val1'] . '"' . "<br />";
}
if($r['attr2'] === 0 || $r['attr2'] === ''){
echo '';
}else{
echo '"2"' . ' "' . $r['attr2'] . ' ; ' . $r['val2'] . '"' . "<br />";
}
if($r['attr3'] === 0 || $r['attr3'] === ''){
echo '';
}else{
echo '"3"' . ' "' . $r['attr3'] . ' ; ' . $r['val4'] . '"' . "<br />";
}
if($r['attr4'] === 0 || $r['attr4'] === ''){
echo '';
}else{
echo '"4"' . ' "' . $r['attr4'] . ' ; ' . $r['val4'] . '"' . "<br />";
}
if($r['attr5'] === 0 || $r['attr5'] === ''){
echo '';
}else{
echo '"5"' . ' "' . $r['attr5'] . ' ; ' . $r['val5'] . '"' . "<br />";
}
if($r['attr6'] === 0 || $r['attr6'] === ''){
echo '';
}else{
echo '"6"' . ' "' . $r['attr6'] . ' ; ' . $r['val6'] . '"' . "<br />";
}
if($r['attr7'] === 0 || $r['attr7'] === ''){
echo '';
}else{
echo '"7"' . ' "' . $r['attr7'] . ' ; ' . $r['val7'] . '"' . "<br />";
}
if($r['attr8'] === 0 || $r['attr8'] === ''){
echo '';
}else{
echo '"8"' . ' "' . $r['attr8'] . ' ; ' . $r['val8'] . '"' . "<br />";
}
if($r['attr9'] === 0 || $r['attr9'] === ''){
echo '';
}else{
echo '"9"' . ' "' . $r['attr9'] . ' ; ' . $r['val9'] . '"' . "<br />";
}
if($r['attr10'] === 0 || $r['attr10'] === ''){
echo '';
}else{
echo '"10"' . ' "' . $r['attr10'] . ' ; ' . $r['val10'] . '"' . "<br />";
}
if($r['attr11'] === 0 || $r['attr11'] === ''){
echo '';
}else{
echo '"11"' . ' "' . $r['attr11'] . ' ; ' . $r['val11'] . '"' . "<br />";
}
if($r['attr12'] === 0 || $r['attr12'] === ''){
echo '';
}else{
echo '"12"' . ' "' . $r['attr12'] . ' ; ' . $r['val12'] . '"' . "<br />";
}
if($r['attr13'] === 0 || $r['attr13'] === ''){
echo '';
}else{
echo '"13"' . ' "' . $r['attr13'] . ' ; ' . $r['val13'] . '"' . "<br />";
}
if($r['attr14'] === 0 || $r['attr14'] === ''){
echo '';
}else{
echo '"14"' . ' "' . $r['attr14'] . ' ; ' . $r['val14'] . '"' . "<br />";
}
if($r['attr15'] === 0 || $r['attr15'] === ''){
echo '';
}else{
echo '"15"' . ' "' . $r['attr15'] . ' ; ' . $r['val15'] . '"' . "<br />";
}
if($r['attr16'] === 0 || $r['attr16'] === ''){
echo '';
}else{
echo '"16"' . ' "' . $r['attr16'] . ' ; ' . $r['val16'] . '"' . "<br />";
}
echo "}";
echo "<br>";
}
}
echo "}";
echo "<br>";
}
echo "<p>" . '}' . "</p>";
What i'm trying to display the text as
custom_weapons_v3
{
STEAM_0:0:1621342
{
"0" //Bat
{
"level" "0"
"quality" "0"
"1" "23 ; 21"
"2" "231 ; 231"
"3" "231 ; 0"
}
"159" //Dalokohs Bar
{
"level" "4"
"quality" "2"
"1" "22 ; 32"
"2" "12 ; 42"
}
}
}
You can use file_put_contents(), but use PHP_EOL instead of <br />.
<?php
file_put_contents('test.html', "<html>". PHP_EOL ."<head><title>Test</title></head>". PHP_EOL ."<body>". PHP_EOL ."<h1>Hello World!</h1>". PHP_EOL ."</body>". PHP_EOL ."</html>");
?>
test.html will be,
<html>
<head><title>Test</title></head>
<body>
<h1>Hello World!</h1>
</body>
</html>
You can use the following code,
<?php
$data = "";
$data .= "custom_weapons_v3". PHP_EOL;
$data .= "{". PHP_EOL;
foreach($arr as $key => $a){
$data .= PHP_EOL
$check = $a['steamid'];
$data .= $check;
$data .= PHP_EOL . "{". PHP_EOL;
foreach($data as $key => $r){
-------------------
-------------------
}
}
$data .= "}" . PHP_EOL . "}";
file_put_contents('output.txt', $data);
?>

Categories