I got this code, which creates a table listing all the files of my directory $filedir. I also got a SQL database which lists the names and custom IDs of every file uploaded. For some reason, it only links me to the subpage with the cusom ID on the first file. The other ones link me to my xampp dashboard. I'd appreciate any kind of help/tips.
<?php
$myDirectory = opendir($filedir);
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
$dirArray2 = array_diff($dirArray, array('.', '..'));
array_splice($dirArray2, 1, 0);
closedir($myDirectory);
$indexCount= count($dirArray2);
echo("<TABLE id=myTable class=table-fill>\n");
echo("<thead><TR><th width=100px>---</th><th width=740px>Name</th><th width=160px>Last Modified</th></TR></thead>\n");
$j = 1;
echo "<tbody id=table class=fixed>";
if (count($dirArray2) == 0) {
echo("<TR>");
echo("<td> -- </td>");
echo("<td>No files uploaded yet</td>");
echo("<td> -- </td>");
echo("</TR>\n");
} else {
for ($i = 0; $i < $indexCount; $i++) {
echo("<TR>");
echo("<td><center><b>" . $j++ . "</b></center></td>");
$filename = $dirArray2[$i];
$owner = $user_data['username'];
$result = mysql_query("SELECT `url` FROM `uploads` WHERE `name` = '$filename' AND `owner` = '$owner'") or die(mysql_error());
$row = mysql_fetch_array($result);
echo("<td class=contextMenu>" . $dirArray2[$i] . "</td>");
echo("<td>" . date("m/d/Y h:ia", filemtime($filedir . $dirArray2[$i])) . "</td>");
echo "</tr>";
}
}
echo "</tbody>";
echo("</TABLE>\n");
?>
Note that I am quite new to PHP and SQL, so be kind please :)
I think there is something wrong with your query. Replace the "`" with "'". This sometimes happens if copy and pasting goes wrong. Also you need to put the variables in between the query, not in the string.
Like so:
"SELECT 'url' FROM 'uploads' WHERE 'name' = '".$filename."' AND 'owner' = '".$owner."'"
If that doesn't solve your problem see how many rows your query returns. You can just run it in your database program.
What I also recommend you do is run a 'select * from uploads', then looping the returned array with a foreach loop.
Something like this:
$content = "";
$result = mysql_query("SELECT * FROM 'uploads'");
$row = mysql_fetch_array($result);
$content.= "<table>";
$content.= "<th>Column1</th><th>Column2</th><th>Column3</th>";
foreach($row as $index => $value){
$content.= "<td>$value[$index][1]</td>";
$content.= "<td>$value[$index][2]</td>";
$content.= "<td>$value[$index][3]</td>";
}
$content.= "</table>";
print $content;
This should return a 3 column table with your data in. Not sure though, haven't tested it.
Related
I confess that I am a beginner. My goal is to be able to summarize in a two-dimensional table the amount per month (in column) for each class (in line). After doing a lot of research, I arrived at this stage in my project.
My problem is , I get the result with months repeating in columns like here.
Here is my code.
This is my sql code : "SELECT classe, mois, sum_ec FROM journal"
This is my php code :
<?php
$tableau = array();
$tblClasse = array();
$rt = mysqli_query($db, $req_sit);//execute la requete
while ($row = $rt->fetch_assoc()){ //forme le tableau
$tableau[$row['classe']][$row['mois']] = $row['sum_ec'];
if (!in_array($row['classe'],$tblClasse)) {
$tblClasse[] = $row['mois'];
}
}
echo '<table border="1">
<tr>
<th> </th>';
foreach ($tblClasse as $classe) {
echo '<th>' . htmlspecialchars($classe) . '</th>';
}
echo '</tr>';
foreach ($tableau as $mois=>$value) {
echo '<tr>';
$new_line = TRUE;
foreach ($tblClasse as $classe) {
if ($new_line) {
echo '<td>' . $mois . '</td>';
$new_line = FALSE;
}
$display = isset($value[$classe]) ? $value[$classe] : " ";
echo '<td>' . $display . '</td>';
}
echo '</tr>';
}
?>
Can someone tell me what mistake I made?
The problem is in this part of your code:
if (!in_array($row['classe'],$tblClasse)) {
$tblClasse[] = $row['mois'];
}
You are adding months (Fev, Mar) to the array and trying to check if it contains classes (PS, MS etc.) which is impossible, and this condition always returns true, so Feb is added to the $tblClasse array at each iteration. You must either check months or classes and add months or classes to the array respectively. And if you want the array to contain unique values (for example, months), use keys instead of values:
if (!array_key_exists ($row['mois'],$tblClasse)) {
$tblClasse[$row['mois']] = 1;
}
Where 1 can be replaced with any other value.
In the further part of your code use the keys, not values of $tblClasse array to form the table heading.
foreach (array_keys ($tblClasse) as $mois) {
echo '<th>' . htmlspecialchars($mois) . '</th>';
}
Eureka! It worked. I just had to change the line for the values to "array_keys"
foreach (array_keys($tblClasse) as $classe) {
if ($new_line) {
echo '<td>' . $mois . '</td>';
$new_line = FALSE;
}
as well and boom! It's perfect. Thank you very much my dear. You have given me a lot of happiness.
I have a little table being duplicated several times based on the number of entries there are in my SQL Database, which let's say is about colours. These tables will go one place to the right with each new entry. Within that, I now want, within each entry, to be able to automate an output for a few fields. For example, a single table entry might be 'reds' and now I want to display the different shades of red, i.e. 'shade1', 'shade2', 'shade3', 'shade4' from that, and then the next entry might be 'yellows' and I would want it to display as many yellows there were added, i.e. 'shade1', 'shade2'. My code is like this:
<?php
error_reporting(E_ALL ^ E_NOTICE);
$stmt = $conn->prepare("SELECT * FROM colours");
$stmt->execute();
$colours = $stmt->fetchALL();
if($colours){
$i = 1;
foreach ($colours as $key => $colour) {
$coloursrow1 .='<table >
<tr><th>' . "<b>{$colour['colour']}</b>" . '</th></tr>
<tr><td>' . "<img src='{$colour['shade1']}' />" . '</td></tr>
<tr><td>' . "<img src='{$colour['shade2']}' />" . '</td></tr>
<tr><td>' . "<img src='{$colour['shade3']}' />" . '</td></tr>
<tr><td>' . "<img src='{$colour['shade4']}' />" . '</td></tr>
</table>';
$i++;
}
}
?>
And I wondered if instead of all the lines like <tr><td>' . "<img src='../colours/reds/shade1.jpg' />" . '</td></tr>, I could put something like
if($colours){
$i = 1; $x = 1;
foreach ($colours as $key => $colour) {
$coloursrow1 .='<table >
<tr><th>' . "<b>{$colour['colour']}</b>" . '</th></tr>';
}
while ($x < 6) {
'<tr><td>' . "<img src='{$colour['shade$x']}' />" . '</td></tr>';
$x++;
}
foreach ($colours as $key => $colour) {
'</table>';
$i++;
}
}
So that it would grab and output them automatically and also giving me the ability to put a cap on how many can be output.
I tried different ways to accomplish this, but I just keep hitting dead ends. I must be doing something wrong(?)
Seems like it would be something like this, since PDO::fetchAll() returns an empty array or false on error. So just iterate over that set directly. Also, the array access is wrong, it should be {$colour['shade'.$x]}.
<?php
error_reporting(E_ALL ^ E_NOTICE);
$stmt = $conn->prepare("SELECT * FROM colours");
$stmt->execute();
$colours = $stmt->fetchALL();
foreach ($colours as $colour) {
$coloursrow1 .= "<table><tr><th><b>{$colour['colour']}</b></th></tr>";
$x = 1;
while ($x < 6) {
$coloursrow1 .= "<tr><td><img src='{$colour['shade'.$x]}' /></td></tr>";
$x++;
}
$coloursrow1 .= '</table>';
}
So I am creating a dynamic menu. Where I have stored the main categories of the menu in a separate table and subcategories in another table.
They are stored in this way
Categories Table
id cat_name
1 HOME,
2 PRODUCTS,
3 SOLUTIONS,
4 NEWS & GALLERY,
5 DOWNLOADS,
6 CONTACT
Right Now I am running a query
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($query)) {
$row['cat_name'];
$cat=explode(",",$row['cat_name']);
}
Then wherever needed I am printing the values like this
<?php echo $cat[0]; .. echo $cat[1]; //and so on ?>
It looks like this right now. And it is supposed to look likethis
But the problem with this solution is that I have to define the index of the array to print it.
I am developing the admin panel for this one and I want that if the admin adds a new menu item then it should automatically fetch it and display it.
But with this solution it is not possible.
I am thinking of iterating the values with a for loop but cannot get it right.
Use foreach ($query as $rows)
or
Use for ($i=0; $i < count($query); $i++)
Probably You can try this also
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($query))
{
$row['cat_name'];
$cat=explode(",",$row['cat_name']);
foreach($cat as $catss)
{
$cat = trim($catss);
$categories .= "<category>" . $cat . "</category>\n";
}
}
You can try this way may be you wish like this for dynamic menu:
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
$menu = '';
$menu .= '<ul>';
while($row = mysqli_fetch_assoc($query))
{
$menu .= '<li>' . $row['cat_name'] . '</li>';
}
$menu .= '</ul>';
echo $menu;
When you are fetching data from table then you can directly print that value using field name. if you want to further use that so, you have to add in one array like:
$cat[] = $row['cat_name'];
then you can use this value in that manner using for or while:
foreach ($cat as $value) {
echo $value;
}
echo '<ul>';
foreach ($cat as $val) {
echo "<li>{$val}</li>";
}
echo '</ul>';
Instead of the index. Also this should be faster and readable than for loop.
You can save it in array then later access the values.
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
$cat = array();
while($row = mysqli_fetch_assoc($query)) {
$cat[]=$row['cat_name']
}
Now:
foreach ($cat as $category) {
$cate_gory = explode("," , $category);
foreach ($cate_gory as $categories) {
echo $categories . "<br>";
}
}
Foreach Loop
foreach($cats as $cat)
{
echo $cat;
}
Basic For Loop
for ($i=0; $i<count($cats); $i++)
{
echo $cats[$i];
}
Simple Use Case
<?php
$cats = 'Stack overflow PHP questions';
$cats = explode(' ',$cats);
$html = '<ul>';
foreach($cats as $cat)
{
$html .= '<li>' . $cat . '</li>';
}
$html .= '</ul>';
echo $html;
?>
Your Code
In this case the while loop you are using will iterate over all the values returned from the sql statement, so there is no need to add more complexity. I would remove the commas from the field names and echo out the html this way.
$sql="SELECT * FROM categories";
$query = mysqli_query($con, $sql);
$html = '<ul>';
while($row = mysqli_fetch_assoc($query)) {
$html .= '<li>' . $row['cat_name'] . '</li>';
}
$html .= '</ul>';
echo $html;
?>
Hi friends am trying to produce random words using order by rand but unable to produce.Here is my code
for($i=0;$i< 10;$i++) {
$result = mysqli_query($conn,"SELECT * FROM questions ORDER BY RAND() LIMIT 2");
if (!$result) {
/*die('Invalid query: ' . mysql_error());*/
die("Query failed".mysqli_error());
}
while ($row = mysqli_fetch_array($result)) {
$meta_descriptions = '{' $row['fact'] . ' ' . '|' $row['fact'] . '|' . $row['fact']}';
echo $meta_descriptions;
}
}
My questions table has one column that is column fact. i t has three values like
apple
ball
cat
Am getting output as
ball |ball| ball only
I want it to be random like
ball|cat|apple
How can I generate it
See this statement inside your while() loop,
$meta_descriptions = '{' $row['fact'] . ' ' . $row['fact'] . '}';
You're using same column value two times in each iteration of while() loop. Simply change your while() code section in the following way,
$meta_descriptions = '';
while ($row = mysqli_fetch_array($result)) {
$meta_descriptions .= $row['fact'] . '|';
}
$meta_descriptions = rtrim($meta_descriptions, '|');
echo $meta_descriptions;
Alternative method:
Create an empty array in the beginning. In each iteration of while() loop, push $row['fact'] value to this array. And finally apply implode() function on this array to get the desired result.
So your code should be like this:
$meta_descriptions = array();
while ($row = mysqli_fetch_array($result)) {
$meta_descriptions[] = $row['fact'];
}
$meta_descriptions = implode('|', $meta_descriptions);
echo $meta_descriptions;
change inside your while loop.
$meta_descriptions = '{';
$descriptions = array();
while ($row = mysqli_fetch_array($result)) {
$descriptions[] = $row['fact'];
}
$meta_descriptions .= implode(" | ",descriptions);
$meta_descriptions .= '}';
echo $meta_descriptions;
I know there are similar questions here and I have read some of the posts and answers, experimented with some of them, but whether it is my limited knowledge of PHP or the peculiarity of my case, I need to ask this.
I am building a dictionary (id, english, bulgarian, theme_id) and would like to group the search results according to theme_id. I am using ORDER BY theme_id, id in my query but I end up displaying the theme with each of the results, while I would like to categorize them as follows:
THEME 1
- result 1
- result 2
THEME 2
- result 3
- result 4
.....
Here is the relevant part of my code:
while($row = mysql_fetch_array($result)) {
//$id = $row['id'];
$english = $row['english'];
$bulgarian = $row['bulgarian'];
$theme_id = $row['theme_id'];
$theme_name = "theme_".$lang;
$theme_query= mysql_query("SELECT theme_id,".$theme_name." FROM ".DICTIONARY_THEMES." WHERE theme_id = ".$theme_id."");
$theme_row = mysql_fetch_array($theme_query);
$theme = $theme_row[$theme_name];
if($source == "english") {
foreach($keywords as $keyword) {
$english = preg_replace("|($keyword)|Ui", "<span style=\"color:#780223\">".$keyword."</span>", $english);
}
$print .= "<li class=\"results-row\">".$theme.": ".$english." = ".$bulgarian."</li>";
}
elseif($source == "bulgarian") {
foreach($keywords as $keyword) {
$bulgarian = preg_replace("|($keyword)|Ui", "<span style=\"color:#780223;\">".$keyword."</span>", $bulgarian);
}
$print .= "<li class=\"results-row\">".$theme.": ".$bulgarian." = ".$english."</li>";
}
}//end while
EDIT: SOLVED, a friend has helped improve my code.
while($row = mysql_fetch_array($result)) {
$english = $row['english'];
$bulgarian = $row['bulgarian'];
$theme_id = $row['theme_id'];
$theme_name = "theme_".$lang;
$theme_query= mysql_query("SELECT theme_id,".$theme_name." FROM ".DICTIONARY_THEMES." WHERE theme_id = ".$theme_id."");
$theme_row = mysql_fetch_array($theme_query);
$theme = $theme_row[$theme_name];
// add all results to an array
$results[] = array(
'english' => $english,
'bulgarian' => $bulgarian,
'theme' => $theme
);
}//end while
$theme = null;
foreach ($results as $result) {
if ($theme != $result['theme']) {
$theme = $result['theme'];
$print .= "<h3>" . $result['theme'] . "</h3>";
}
if ($source == "english") {
foreach ($keywords as $keyword) {
$result['english'] = preg_replace("|($keyword)|Ui", "<span style=\"color:#780223\">" . $keyword . "</span>", $result['english']);
}
$print .= "<li class=\"results-row\">" . $result['english'] . " = " . $result['bulgarian'] . "</li>";
} elseif ($source == "bulgarian") {
foreach ($keywords as $keyword) {
$result['bulgarian'] = preg_replace("|($keyword)|Ui", "<span style=\"color:#780223;\">" . $keyword . "</span>", $result['bulgarian']);
}
$print .= "<li class=\"results-row\">" . $result['bulgarian'] . " = " . $result['english'] . "</li>";
}
}
Based on your code, you should first add all items to an array and afterwards print that array in a separate function/block.
Right after your second MySQL query, put something like
$output[$theme_id] = mysql_fetch_array( $theme_query );
and move all the following code out of the outer result-while-loop (while($row = mysql_fetch_array($result))).
But in fact I would try to put a MySQL query together, to have an ordered, grouped result with all the selected themes in all languages and without querying the database inside a loop.
Or you use something existing, for example this singleton Lexicon class.