How to put a database into an array - arrays

I finally succeeded in making an image carousel by using php.
The code below works, but there are no images in it.
Now I am trying to make this array to get data from a Mysql database.
Could you show me an example of how do I do it?
<?php
//MySQL database connection
$mysqli = new mysqli('localhost', 'root', '', 'webshop');
$SQL = "SELECT image FROM productlist LIMIT 5";
$result = $mysqli->query($SQL);
$array = array(
0 => "picture1.jpg",
1 => "picture2.jpg",
2 => "picture3.jpg",
3 => "picture4.jpg",
4 => "picture5.jpg",
5 => "kalle6.jpg",
);
//Goes to previous page when clicking on prev
$index = $_GET['start'];
if ($index > 0) {
echo ' prev ';
} else {
echo ' prev ';
}
//Display 3 images
$show_img = 3;
$num_img = 0;
for($i = $_GET['start']; $i<count($array) && $num_img < $show_img; $i++) {
$num_img++;
echo "<img src=".$array[$i]."/>\n";
}
for($i=$num_img; $i<$show_img;$i++) {
echo "<img src=".(count($array) - 1)."/>\n";
}
//Goes to next page when clicking on next
if ($index < count($array) - 1) {
echo ' next ';
} else {
echo ' next ';
}
?>

You should have a look at this: http://php.net/manual/en/mysqli-result.fetch-array.php
/* numeric array */
/* $row is your $array */
$row = $result->fetch_array(MYSQLI_NUM);
echo "<pre>";
print_r($row);
echo "<pre>";
This should help you get the result you want

Related

How to fetch all the coinbase pro transactions correctly in php

I have a question of coinbase pro,
How can I get all the orders/transactions from coinbase pro using php without any limit , or with any limit parameter.
I am using the following library to fetch all the coinbase pro orders , but it only gives me 100 orders from the below method.
fetch_orders
Here is the method,
$CoinbaseProtrans = $CoinbasePro->fetch_orders('ETH/USD',"",150,array('limit'=>100,'after'=>1));
echo "<pre>";print_r($CoinbaseProtrans);echo "</pre>";
Here is the error I am getting,
Fatal error: Uncaught ccxt\ExchangeError: coinbasepro after cursor value is not valid in coinbasepro.php:813
Here is the link of the library:
https://github.com/ccxt/ccxt/blob/master/php/coinbasepro.php
This question was answered here: https://github.com/ccxt/ccxt/issues/6105#issuecomment-552405563
<?php
include_once ('ccxt.php');
date_default_timezone_set ('UTC');
$exchange = new \ccxt\coinbasepro(array(
'apiKey' => 'YOUR_API_KEY',
'secret' => 'YOUR_SECRET',
// 'verbose' => true, // uncomment for debugging
// https://github.com/ccxt/ccxt/wiki/Manual#rate-limit
'enableRateLimit' => true, // rate-limiting is required by the Manual
));
$exchange->load_markets ();
// $exchange->verbose = true; // uncomment for debugging
$all_results = array();
$symbol = 'ETH/USD';
$since = null;
$limit = 100;
$params = array();
do {
// any of the following methods should work:
// $results = $exchange->fetch_orders($symbol, $since, $limit, $params);
// $results = $exchange->fetch_my_trades($symbol, $since, $limit, $params);
$results = $exchange->fetch_trades($symbol, $since, $limit, $params);
echo $exchange->iso8601($exchange->milliseconds());
echo ' fetched ' . count($results) . " results\n";
$all_results = array_merge ($all_results, $results);
if (count($results) > 0) {
$last = count($results) - 1;
echo ' last result ' . $results[$last]['id'] . ' ' . $results[$last]['datetime'] . "\n";
echo ' first result ' . $results[0]['id'] . ' ' . $results[0]['datetime'] . "\n";
} else {
break;
}
#$params['after'] = $exchange->last_response_headers['cb-after'][0];
// uncomment one of the following:
// } while (true); // fetch all results forever
} while (count($all_results) < 1000); // fetch up to 1000 results
echo "fetched " . count($all_results) . " results in total\n";
?>

Php keep checkboxes checked after form error

How can i keep the selected checkboxes checked, after the form submit on error? Now, i get this error message: Warning: in_array() expects parameter 2 to be array, string given in
Thanks!
function runsql_array($sql)
{
global $kapcs;
$res = mysqli_query($kapcs , $sql) or die(mysqli_error( $kapcs));
if (mysqli_num_rows($res) == 0)
{
return array();
}
else
{
$out = array();
while ($a = mysqli_fetch_assoc($res))
{
$out[] = $a;
}
return $out;
}
}
<td>
<?php
$ertek = isset($_POST["termek_tul_tipusok"]) ? $_POST["termek_tul_tipusok"] : '' ;
$values = runsql_array("SELECT termek_tipus_id, termek_tipus_nev FROM termek_tipusok WHERE termek_tipus_status = 1
ORDER BY termek_tipus_nev ASC");
foreach($values as $val=>$szoveg)
{
$checked = in_array($val, $ertek) ? ' checked ' : '' ;
echo '<div style="margin:4px 0;"><label style="cursor:pointer;" for="tulajdonsag-'.$val.'">';
echo '<input id="tulajdonsag-'.$val.'" type="checkbox" name="termek_tul_tipusok[]" '.$checked.' value="'.$val.'" />';
echo $szoveg['termek_tipus_nev'];
echo '</label></div>';
}
?>
</td>
the line
$ertek = isset($_POST["termek_tul_tipusok"]) ? $_POST["termek_tul_tipusok"] : '' ;
defines $ertek. And in the case that the post dosnt exist its defined as an empty string. You should use an empty Array.
$ertek = isset($_POST["termek_tul_tipusok"]) ? $_POST["termek_tul_tipusok"] : array() ;

How to get selected value in dropdownlist by using cakephp?

My code is something like below :
public function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
And here is the drop down list code :
$this->Form->select('ClubOpenDay.0.open_time', $this->makeEntryTime(), array("empty" => false, 'class' => 'input-medium'));
My problem is I am getting the value like 11 am, 12 am.But I want to make it selected when I will get value 11 am or 12 am from database.Any idea how can I do this?
Please try this:
<?php echo $this->Form->select('ClubOpenDay.0.open_time',$this->makeEntryTime(),array("empty" => false,'value' => $value_selected,'class' => 'input-medium')); ?>
"$value_selected" is any value with in the input range.
i.e what $this->makeEntryTime() returns.
Thanks
I assume your method is in view which you shouldn't do this. remove public from your function and remove $this to call your function.
try to keep your method inside a controller at least. Here is if you want to keep your method in view.
function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
echo $this->Form->select('selete_id', makeEntryTime(), array("empty" => false, 'class' => 'input-medium'));
but if you want to make it MVC.
//controller
function makeEntryTime($first = '') {
$j = 0;
if (empty($first)) {
$j = 1;
} else {
$entry[$j] = $first;
$j++;
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' am'] = $i . ' am';
}
for ($i = $j; $i <= 12; $i++) {
$entry[$i . ' pm'] = $i . ' pm';
}
return $entry;
}
//page function - for examlpe add() method
public function add(){
$this->set('times', $this->makeEntryTime());
}
//view of add method
echo $this->Form->select('time_id', $times, array("empty" => false, 'class' => 'input-medium'));

Undefined offset: 1 twitch channel grab

I get this on my website:
Notice: Undefined offset: 1 in ...
Here is the full code, bolded is the part it refers to i think, basically this scripts should grab list of my channels from ther DB and then display info from the twitch:
<?php
defined('_JEXEC') or die('Direct access to this location is not allowed.');
$userList = $params->get('userlist');
$usersArray = explode(',', $userList);
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
$checkedOnline = array ();
foreach($usersArray as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
//used to be $viewer = $json_array[$i]['stream_count'];
**foreach($usersArray as $i =>$value){
$title = $json_array[$i]['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$name = $json_array[$i]['name'];
$game = $json_array[$i]['meta_game'];
$viewer = $json_array[$i]['channel_count'];
$topic = $json_array[$i]['title'];
onlinecheck($member, $viewer, $topic, $game);
$checkedOnline[] = signin($member);
}**
unset($value);
unset($i);
function onlinecheck($online, $viewers, $topic, $game)
{
if ($game == "Counter-Strike: Global Offensive")
{
$igra = "csgo12.jpg";
}
else{
$igra = "online.png";
}
if ($online != null)
{
echo ' <img src="./modules/mod_twitchlist/tmpl/'.$igra.'">';
echo ' <strong>'.$online.'</strong>';
echo ' (' .$viewers.') - ';
echo '<strong>'.$topic.'</strong> </br>';
}
}
function signin($person){
if($person != null){
return $person;
}
else{
return null;
}
}
?>
<!-- <hr> -->
<?php
foreach ($usersArray as $i => $value1) {
foreach($checkedOnline as $ii => $value2){
if($value1 == $value2){
unset($usersArray[$i]);
}
}
}
$broj1 = count($usersArray); //neaktivni korisnici
$broj2 = count($checkedOnline); //ukupno streamova
if ($broj1 == $broj2){
echo '<strong><center>Nijedan stream nije aktivan trenutno!</center></strong>';
}
?>
Any hints?

Expanding Rows for Unique Checkboxes

I was just recently given a project for my job to write a script that compares two mysql databases and print out information into an html table. Currently, I am trying to insert a checkbox by each individual's name and when selected, rows pertaining to that individual will expand underneath the person's name. I am combining javascript into my script to do this, although I really have no experience is it. The problem I am having is that when any checkbox is selected, all the rows for each individual is expanding instead of the rows pertaining only to the one individual selected.
Here is my code so far:
PHP Code:
<?php
$link = mysql_connect ($server = "harris.lib.fit.edu", $username = "", $password = "") or die(mysql_error());
$db = mysql_select_db ("library-test") or die(mysql_error());
$ids = mysql_query("SELECT * FROM `ifc_studylog`") or die(mysql_error()); //not single quotes (tilda apostrophy)
$x=0;
$n=0;
while($row = mysql_fetch_array( $ids ))
{
$tracksid1[$x] = $row['fitID'];
$checkin[$x] = $row['checkin'];
$checkout[$x] = $row['checkout'];
$n++;
$x++;
}
$names = mysql_query("SELECT * FROM `ifc_users`") or die(mysql_error()); //not single quotes (tilda apostrophy)
$x=0;
while($row = mysql_fetch_array( $names ))
{
$tracksnamefirst[$x] = $row['firstName'];
$tracksnamesecond[$x] = $row['lastname'];
$tracksid2[$x] = $row['fitID'];
$tracksuser[$x] = $row['tracks'];
$x++;
}
$x=0;
foreach($tracksid2 as $comparename)
{
$chk = strval($x);
?>
JS Code:
$(window).load(function() {
$('.varx').click(function() {
$('.text').toggle(this.checked);
});
});​
PHP Code:
<?php
echo '<td><input id = "<?=$chk?>" type="checkbox" class="varx" /></td>';
echo '<td align="center">'.$comparename.'</td>';
echo'<td align="center">'.$tracksnamefirst[$x].'</td>';
echo'<td align="center">'.$tracksnamesecond[$x].'</td>';
$z=0;
foreach($tracksid1 as $compareid)
{
$HH=0;
$MM =0;
$SS =0;
if($compareid == $comparename)// && $tracks==$tracksuser[$x])
{
$SS = sprintf("%02s",(($checkout[$z]-$checkin[$z])%60));
$MM = sprintf("%02s",(($checkout[$z]-$checkin[$z])/60 %60));
$HH = sprintf("%02s",(($checkout[$z]-$checkin[$z])/3600 %24));
// echo'<td align="center">'.$HH.':'.$MM.':'.$SS.'</td>';
echo '</tr>';
echo '<tr>';
echo "<td id='txt' class='text' align='center' colspan='2' style='display:none'></td>";
echo "<td id='txt' class='text' align='center' style='display:none'>".$checkin[$z]."</td>";
echo '</tr>';
}
echo '<tr>';
$z++;
echo '</tr>';
}
$x++;
}
}
?>
Any help is appreciated and apologies if I am too vague on the subject. The username and password have been left out for security reasons.

Resources