Can any one help me? I found difficult about create matrix dynamic using PHP programming. I want value of matrix dynamic from mysql database.
Such as example :
INF 56 78 67
45 INF 78 67
45 56 INF 67
45 56 78 INF
This code it :
function random()
{
$max = 7;
if (isset($_POST['amount']))
{
$amount = (int)$_POST['amount'];
$rows = ($amount > 2 && $amount < 15) ? $amount : 3;
}
else
{
$rows = rand(3, 7);
}
for ($i = 1; $i <= $rows; $i++)
{
for ($j = 1; $j <= $rows; $j++)
{
$this->table[$i][$j] = $i == $j ? INF : rand(0, 99);
}
}
}
function __toString()
{
$str = '<table class="table table-bordered" id="tableInput"><tbody>';
$str .= '<tr><td></td>';
foreach ($this->table as $rowName => $row)
{
$str .= "<td>$rowName</td>";
}
$str .= '</tr>';
foreach ($this->table as $rowName => $row)
{
$str .= "<tr><td>$rowName</td>";
foreach ($row as $columnName => $value)
{
$str .= "<td>";
$str .=
'<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
$columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
$str .= "</td>";
}
$str .= '</tr>';
}
$str .= '</tbody></table>';
return $str;
}
Thanks
Related
I want to calculate the sum and output a two columns table. But, the format is wrong. The numbers are all packed in one line.
<td>
<?php
for ($j = 1; $j <= 10; $j++)
{
print "<tr>";
print $j;
print "</tr>";
}
?>
</td>
<td>
<?php
$sum = 0;
for($i = 1; $i<=10; $i++)
{
print "<tr>";
print $sum = $sum + $i;
print "</tr>";
}
?>
</td>
Here, we would be writing one or two for loops, join our desired HTML string, and finally we would print it.
Test:
<?php
$html = '<tr>';
for ($j = 1; $j <= 10; $j++) {
$html .= "<td>" . $j . "</td>";
}
$html .= '</tr>';
$html .= '<tr>';
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
$html .= "<td>" . $sum . "</td>";
}
$html .= '</tr>';
print($html);
?>
We would be also adding table open and close tags (<table>, </table>):
<?php
$html = '<table>';
$html .= '<tr>';
$html .= "<td>Number</td>";
for ($j = 1; $j <= 10; $j++) {
$html .= "<td>" . $j . "</td>";
}
$html .= '</tr>';
$html .= '<tr>';
$html .= "<td>Cumulative Sum</td>";
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
$html .= "<td>" . $sum . "</td>";
}
$html .= '</tr>';
$html .= '</table>';
print($html);
?>
For two columns, we would just use one for` loop:
<?php
$html = '<table>';
$html .= "<tr><th>Number</th><th>Cumulative Sum</th></tr>";
for ($j = 1; $j <= 10; $j++) {
$html .= "<tr>";
$html .= "<th>" . $j . "</th>";
$sum += $j;
$html .= "<th>" . $sum . "</th>";
$html .= "</tr>";
}
$html .= '</table>';
print($html);
I ended up here when looking to get a cumulative sum in PHP. For the ones like me, I wrote the following function. It's safe for empty arrays as well.
function cumsum ($arr) {
return array_reduce($arr, function ($c, $i) { $c[] = end($c) + $i; return $c; }, []);
}
For my dynamic navigation I need some help, since I'm still new to PHP.
Using these functions, this file passes the values of the array to a json and a js file.
The correct indexing is particularly important for the function.
$_js_remc->remote_copy($renaming_js_file, $renaming_js_file_to);
$_json_remc->remote_copy($renaming_jsonfile, $renaming_jsonfile_to);
if ($_js_remc->remote_is_writeable($renaming_js_file) && $_json_remc->remote_is_writeable($renaming_jsonfile)) {
$i = 10000;
$menu_string = "document.getElementById('categories_menu').innerHTML='";
for ($z = 0; $z < count($_POST['menu_lvl1_text']); $z++) {
$i = $i + 100;
$menu_string .= '<li>' . htmlentities($_POST['menu_lvl1_text'][$z], ENT_QUOTES, "UTF-8") . '';
$linkname_lvl1 = ($_POST['menu_lvl1_text'][$z]);
$url_lvl1 = ($_POST['menu_lvl1_link'][$z]);
$menu_list[$i] = array($linkname_lvl1 => $url_lvl1);
if (isset($_POST['menu' . $z . '_lvl2_text']) && !empty($_POST['menu' . $z . '_lvl2_text'])) {
$i = $i +1;
$menu_string .= '<ul>';
for ($zs = 0; $zs < count($_POST['menu' . $z . '_lvl2_text']); $zs++) {
$menu_string .= '<li>' . htmlentities($_POST['menu' . $z . '_lvl2_text'][$zs], ENT_QUOTES, "UTF-8") . '';
$linkname_lvl2 = ($_POST['menu' . $z . '_lvl2_text'][$zs]);
$url_lvl2 = ($_POST['menu' . $z . '_lvl2_link'][$zs]);
$menu_list[$i] = array($linkname_lvl2 => $url_lvl2);
$i = $i + 1;
}
$menu_string .= '</ul>';
}
$menu_string .= '</li>';
}
$menu_string .= "';";
As you can see from the code, the json file logically logs the following values.
{
"10100": {
"Lvl_1_1": "Level_1_1"
},
"10101": {
"Lvl1_2": "Lvl1_2"
},
"10102": {
"Lvl1_3": "Lvl1_3"
},
"10203": {
"Lvl_2_1": "Lvl_2_1"
},
"10204": {
"Lvl2_2": "Lvl2_2"
}
}
Problem:
The Sublevel (for ex. "10102") is, of course, to be incremented, but only within it's category, after which the index for the next category shell resume, for example, "10200". Each category has different numbers of sublinks.
The output will look like:
<li id="u_1_0">
<span id="i_1_0"></span>
<ul class="sub">
<li id="u_1_2"><span id="i_1_2"></span></li>
<li id="u_1_3"><span id="i_1_3"></span></li>
Does someone know advice?
If you struggle only with resetting your counter $i, you could just reset the counter after the inner for loop:
if ($_js_remc->remote_is_writeable($renaming_js_file) && $_json_remc->remote_is_writeable($renaming_jsonfile)) {
$i = 10000;
$menu_string = "document.getElementById('categories_menu').innerHTML='";
for ($z = 0; $z < count($_POST['menu_lvl1_text']); $z++) {
$i = $i + 100;
$menu_string .= '<li>' . htmlentities($_POST['menu_lvl1_text'][$z], ENT_QUOTES, "UTF-8") . '';
$linkname_lvl1 = ($_POST['menu_lvl1_text'][$z]);
$url_lvl1 = ($_POST['menu_lvl1_link'][$z]);
$menu_list[$i] = array($linkname_lvl1 => $url_lvl1);
if (isset($_POST['menu' . $z . '_lvl2_text']) && !empty($_POST['menu' . $z . '_lvl2_text'])) {
$oldCounter = $i;
$menu_string .= '<ul>';
for ($zs = 0; $zs < count($_POST['menu' . $z . '_lvl2_text']); $zs++) {
$i = $i + 1;
[...use $i as before...]
}
$i = $oldCounter;
$menu_string .= '</ul>';
}
$menu_string .= '</li>';
}
$menu_string .= "';";
Notice that your ids are not unique. If there are 100 sublinks, you are in trouble!
However, I don't understand your second code block and how it is related to your question.
I have this code:
function random()
{
include('config/koneksi.php');
$result = mysql_query("select * from temp_hasil");
$n =mysql_num_rows(mysql_query("SELECT * FROM temp_hasil"));
for ($i = 1; $i <= $n; $i++)
{
for ($j = 1; $j <= $n; $j++)
{
$rows = mysql_fetch_array($result);
$this->table[$i][$j] = $i == $j ? INF : $rows['id'];
}
}
}
function __toString()
{
$str = '<table class="table table-bordered" id="tableInput"> <tbody>';
$str .= '<tr><td></td>';
foreach ($this->table as $rowName => $row)
{
$str .= "<td>$rowName</td>";
}
$str .= '</tr>';
foreach ($this->table as $rowName => $row)
{
$str .= "<tr><td>$rowName</td>";
foreach ($row as $columnName => $value)
{
$str .= "<td>";
$str .=
'<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
$columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
$str .= "</td>";
}
$str .= '</tr>';
}
$str .= '</tbody></table>';
return $str;
}
}
$str .= '</tr>';
foreach ($this->table as $rowName => $row)
{
$str .= "<tr><td>$rowName</td>";
foreach ($row as $columnName => $value)
{
$str .= "<td>";
$str .=
'<input class="form-control" type="text" value="' . $value . '" name="table[' . $rowName . '][' .
$columnName . ']" requied' . ($columnName == $rowName ? ' disabled' : '') . '>';
$str .= "</td>";
}
$str .= '</tr>';
}
$str .= '</tbody></table>';
return $str;
}`
and I have table "temp_hasil" such as:
id_temp | id |
1 | 8 |
2 | 5 |
3 | 7 |
If I run this code, it result :
1 2 3
1 | INF | 5 | 7 |
2 | | INF| |
3 | | | INF|
But I want end result such as:
1 2 3
1 | INF | 5 | 7 |
2 | 8 | INF| 7 |
3 | 8 | 5 | INF|
How do I code it in php? Is there anything wrong code? Thanks,... :)
Problem is - You can only fetch complete results once and can't get the first record in second loop. So instead, store the results in an array and use the array to create matrix.
This should work. Try this.
function random()
{
include('config/koneksi.php');
$result = mysql_query("select * from temp_hasil");
$n =mysql_num_rows(mysql_query("SELECT * FROM temp_hasil"));
$r=array();
for ($a = 1; $a <= $n; $a++)
{
$rows = mysql_fetch_array($result);
$r[]=$rows['id'];
}
for ($i = 1; $i <= $n; $i++)
{
for ($j = 1; $j <= $n; $j++)
{
$this->table[$i][$j] = $i == $j ? INF : $r[$j-1];
}
}
}
Simply, you can use a while loop and store to array and then use array sizeof($r) or count($r) in for loop. Which ever you prefer.
function random()
{
include('config/koneksi.php');
$result = mysql_query("select * from temp_hasil");
$r=array();
while($rows = mysql_fetch_array($result)) {
$r[]=$rows['id'];
}
for ($i = 1; $i <= sizeof($r); $i++)
{
for ($j = 1; $j <= sizeof($r); $j++)
{
$this->table[$i][$j] = $i == $j ? INF : $r[$j-1];
}
}
}
I have a follow up question on something I got help with here the other day (No Table Three Column Category Layout).
The script is as follows:
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$parent_node = mysql_fetch_assoc($res);
$id = (isset($parent_node['cat_id'])) ? $parent_node['cat_id'] : $id;
$catalist = '';
if ($parent_node['left_id'] != 1)
{
$children = $catscontrol->get_children_list($parent_node['left_id'], $parent_node['right_id']);
$childarray = array($id);
foreach ($children as $k => $v)
{
$childarray[] = $v['cat_id'];
}
$catalist = '(';
$catalist .= implode(',', $childarray);
$catalist .= ')';
$all_items = false;
}
$NOW = time();
/*
specified category number
look into table - and if we don't have such category - redirect to full list
*/
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE cat_id = " . $id;
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$category = mysql_fetch_assoc($result);
if (mysql_num_rows($result) == 0)
{
// redirect to global categories list
header ('location: browse.php?id=0');
exit;
}
else
{
// Retrieve the translated category name
$par_id = $category['parent_id'];
$TPL_categories_string = '';
$crumbs = $catscontrol->get_bread_crumbs($category['left_id'], $category['right_id']);
for ($i = 0; $i < count($crumbs); $i++)
{
if ($crumbs[$i]['cat_id'] > 0)
{
if ($i > 0)
{
$TPL_categories_string .= ' > ';
}
$TPL_categories_string .= '' . $category_names[$crumbs[$i]['cat_id']] . '';
}
}
// get list of subcategories of this category
$subcat_count = 0;
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE parent_id = " . $id . " ORDER BY cat_name";
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$need_to_continue = 1;
$cycle = 1;
$column = 1;
$TPL_main_value = '';
while ($row = mysql_fetch_array($result))
{
++$subcat_count;
if ($cycle == 1)
{
$TPL_main_value .= '<div class="col'.$column.'"><ul>' . "\n";
}
$sub_counter = $row['sub_counter'];
$cat_counter = $row['counter'];
if ($sub_counter != 0)
{
$count_string = ' (' . $sub_counter . ')';
}
else
{
if ($cat_counter != 0)
{
$count_string = ' (' . $cat_counter . ')';
}
else
{
$count_string = '';
}
}
if ($row['cat_colour'] != '')
{
$BG = 'bgcolor=' . $row['cat_colour'];
}
else
{
$BG = '';
}
// Retrieve the translated category name
$row['cat_name'] = $category_names[$row['cat_id']];
$catimage = (!empty($row['cat_image'])) ? '<img src="' . $row['cat_image'] . '" border=0>' : '';
$TPL_main_value .= "\t" . '<li>' . $catimage . '' . $row['cat_name'] . $count_string . '</li>' . "\n";
++$cycle;
if ($cycle == 7) // <---- here
{
$cycle = 1;
$TPL_main_value .= '</ul></div>' . "\n";
++$column;
}
}
if ($cycle >= 2 && $cycle <= 6) // <---- here minus 1
{
while ($cycle < 7) // <---- and here
{
$TPL_main_value .= ' <p> </p>' . "\n";
++$cycle;
}
$TPL_main_value .= '</ul></div>'.$number.'
' . "\n";
}
I was needing to divide the resulting links into three columns to fit my html layout.
We accomplished this by changing the numbers in the code marked with "// <---- here".
Because the amount of links returned could be different each time, I am trying to figure out how to change those numbers on the fly. I tried using
$number_a = mysql_num_rows($result);
$number_b = $number_a / 3;
$number_b = ceil($number_b);
$number_c = $number_b - 1;
and then replacing the numbers with $number_b or $number_c but that doesn't work. Any ideas?
As mentioned before, you can use the mod (%) function to do that.
Basically what it does is to get the remainder after division. So, if you say 11 % 3, you will get 2 since that is the remainder after division. You can then make use of this to check when a number is divisible by 3 (the remainder will be zero), and insert an end </div> in your code.
Here is a simplified example on how to use it to insert a newline after every 3 columns:
$cycle = 1;
$arr = range (1, 20);
$len = sizeof ($arr);
for ( ; $cycle <= $len; $cycle++)
{
echo "{$arr[$cycle - 1]} ";
if ($cycle % 3 == 0)
{
echo "\n";
}
}
echo "\n\n";
this is the first time I'm using PDO and I'm trying to create a function to inserting data in a table just by passing 3 parameters.
This is the function:
public function insert($t, $v, $r)
{
if ($this->active)
{
if (($t != null) && ($r != null) && ($v != null) && (count($v) == count($r)))
{
$instruction = 'INSERT INTO `' . DBNAME . '`.`' . $t . '` (';
for ($i = 0; $i < count($r); $i++)
$_r[$i] = '`' . $r[$i] . '`';
$_r = implode(',', $_r);
$instruction .= $_r . ') VALUES (';
for ($i = 0; $i < count($r); $i++)
$r[$i] = ':' . $r[$i];
$r = implode(',', $r);
$instruction .= $r . ');';
$statement = $this->PDO->prepare($instruction);
for ($i = 0; $i < count($r); $i++)
$statement->bindParam($r[$i], $v[$i]);
$statement->execute();
echo $instruction;
return true;
} else
return false;
} else
return false;
}
I tried this:
$t = "users";
$v = array(201);
$r = array("id_user");
$data->insert($t, $v, $r);
But it doesn't work. That's what it returns:
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /Applications/XAMPP/xamppfiles/htdocs/friz/mysql_functions.php on line 68
INSERT INTO friz.users (id_user) VALUES (:id_user);
Can you help me?
P.S.: I used the search function, but I didn't find anything to solve my problem.
Your line
$r = implode(',', $r);
converts $r from an array to a string;
so it no longer contains an array of names when you try to bind; so $r[$i] will simply be the first character of the string $r which is a ':'
for ($i = 0; $i < count($r); $i++)
$_r1[$i] = '`' . $r[$i] . '`';
$_r1 = implode(',', $_r1);
$instruction .= $_r1 . ') VALUES (';
for ($i = 0; $i < count($r); $i++)
$_r2[$i] = ':' . $r[$i];
$_r2 = implode(',', $_r2);
$instruction .= $_r2 . ');';
will leave $r as an array