I have the following query and I'm trying to select only the rows where I have picture.
In table kat_gl_picture I have 3 categories, but I don't have picture in all 3 categories yet!
All work just fine, but I have printed name of third category, where I don't have picture.
I tried WHERE link LIKE '%$first_var%'AND NOT (link <=> NULL)
....IS NOT NULL - but nothing yet worked.
Tabele1 and 2 and web problem solved
<?
include("connection.php");
$kategorije = mysql_query("SELECT * FROM kat_gl_picture ORDER BY rbr");
while ($red=mysql_fetch_array($kategorije))
{
$first_var = $red['kat'];
$result = mysql_query("SELECT id, naziv, ime, tekst, username, link, file_name, datum FROM Tab_Pic_Pic
WHERE link LIKE '%$first_var%'
ORDER BY id");
echo '<table>';
echo '<tbody>';
echo $first_var;
echo '<tr>';
echo '<TD valign="top">';
while ($row=mysql_fetch_array($result))
{
list($x, $y) = getimagesize("admin /upload/".$row['file_name']);
if ($x>$y) {
$y=($y/$x)*150;
$x=150;
}
else
{
$x=($x/$y)*115;
$y=115;
}
$ID_broj = $row["id"];
$tekst_broj = $row["tekst"];
?>
<? echo '<img src="admin /upload/'.$row['file_name'].'" height="'.$y.'" width="'.$x.'"/>';?>
<?
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
The problem is that I cannot determine if there is a foreign key in your Tab_Pic_Pic table. You can however join on a LIKE.
Try this:
SELECT * FROM Tab_Pic_Pic JOIN kat_gl_picture ON Tab_Pic_Pic.link LIKE CONCAT('%', kat_gl_picture.kat, '%') ORDER BY kat_gl_picture.kat, Tab_Pic_Pic, rbr
It should give you a list of all pictures including the kat_gl_picture.kat field which you can just use a local variable to detect change.
Would be easier to provide a more accurate example if I had the full table info. Standard method is to have a foreign key in Tab_Pic_Pic that references the corresponding primary key from the kat_gl_picture table.
Alternatively, if you simply want to omit the blank category it can be done in your PHP code like this:
if(mysqli_num_rows($result)>0){
echo '<table>';
echo '<tbody>';
echo $first_var;
echo '<tr>';
echo '<TD valign="top">';
while ($row=mysql_fetch_array($result))
{
list($x, $y) = getimagesize("admin /upload/".$row['file_name']);
if ($x>$y) {
$y=($y/$x)*150;
$x=150;
}
else
{
$x=($x/$y)*115;
$y=115;
}
$ID_broj = $row["id"];
$tekst_broj = $row["tekst"];
?>
<? echo '<img src="admin /upload/'.$row['file_name'].'" height="'.$y.'" width="'.$x.'"/>';?>
<?
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
Wrapping the output of your HTML in a simple condition like this should illustrate that the script itself is inefficient. Trust me when I say it can all be accomplished with one query. Do not get frustrated. You'll figure it out. Programming is hard.
Related
I need to get the values of the selected select
like the orderno of the changed row
The table looks like this
if I change order no 1002 into sold
$query = mysql_query("select * from orders");
while($row = mysql_fetch_assoc($query))
{
echo '<tr>';
echo '<td>'.$row['orderno'].'</td>';
echo '<td>'.$row['total'].'</td>';
echo '<td>';
if($row['status'] == "sold")
{
echo '<select name = "status[]">';
echo '<option value = "sold" selected>Sold</option>';
echo '<option value = "cancelled">Cancelled</option>';
echo '</select>';
}
else
{
echo '<select name = "status[]">';
echo '<option value = "sold">Sold</option>';
echo '<option value = "cancelled" selected>Cancelled</option>';
echo '</select>';
}
echo '</td>
echo </tr>';
}
if(isset($_POST['status']))
{
//sample only
echo "You change orderno ".$orderno_here." with the
total of ".$total_of_order." into ".$selected_status_of_orderno_here;
}
There are many records in the table and the selects are the same
how do I get the values of the selected select
Following your question, whatever i understood, according to that i am supposing you are getting data from form like tags in your page. If that is the case, you know that which value is changed to be reflected in db. So you can get that selected select from this.
you can check the change through this code:
if(isset($_POST['element'])){
//if this block executed, the <element> named element is changed
}
ignore this answer if this is not the case you are using in your code..
I'm actually trying to use JOINS for the first time and I'm having a tough time getting it to go. I have two tables...stories and wp_users and I'm trying to return all stories and include the display_name of the user from users along with each story.
This code works fine to get all results from stories and show story name and genre:
$results = $wpdb->get_results("SELECT * FROM stories where stories.active = 1");
foreach ($results as $row) {
echo '<tr>';
echo '<td>' . $row->story_name; '</td>';
echo '<td>' . $row->genre; '</td>';
Now I want to also include the name of the user who wrote the story ("display_name" from wp_users table)
After reading many sites about joins the below approach seemed best, but sadly it returns no results:
<?php
global $wpdb;
$sql = "SELECT stories.story_name, stories.genre, wp_users.display_name as display FROM
stories LEFT JOIN wp_users ON stories.ID=wp_users.ID where stories.active = 1";
$results = $wpdb->query($sql);
if($results->num_rows) {
while($row = $results->fetch_object()) {
echo "{$row->story_name} {$row->genre} {$row->display}<br>";
}
}
else {
echo 'No results';
}
Here are some suggestions that might help:
In your first code snippet, you are using $wpdb->get_results() while in your second snippet there is $wpdb->query(). There might be some difference in the returned value and the meaning of the parameters.
Find out about the method that asks for error messages after querying and use it to dump the message. Often if no rows are returned it is because there was an error.
Make sure num_rows really is got from $results. Here (http://codex.wordpress.org/Function_Reference/wpdb_Class) it says the expression is $wpdb->num_rows. (You'll find it by searching the site for 'num_rows', it'll scroll down.)
Try to use the MySQL Command Shell or some PhpMyAdmin sandbox to check the query. Entering the SQL directly will show typos and other problems like "unknown column" in the SQL code, and it allows to edit fast. Joining syntax varies between databases, yet your LEFT JOIN is ok to MySQL. There seems to by no problem on the syntactic level.
This docu http://codex.wordpress.org/Function_Reference/wpdb_Class may help to come clear with the code.
Got it!
<?php
global $wpdb;
$results = $wpdb->get_results("SELECT stories.story_name, stories.genre,
wp_users.display_name FROM stories LEFT JOIN wp_users ON stories.ID=wp_users.ID where
stories.active = 1");
if ($results) {
echo '<table>';
echo '<tr>';
echo '<td><b>Story Name</b></td>';
echo '<td><b>Genre</b></td>';
echo '<td><b>Last User on this Website to See</b></td>';
echo '</tr>';
foreach ($results as $row) {
echo '<tr>';
echo '<td>' . $row->story_name; '</td>';
echo '<td>' . $row->genre; '</td>';
echo '<td>' . $row->display_name; '</td>';
echo '</tr>';
}
echo '</table>';
}
?>
I want convert $RowNumber from string to a variable, for use in while, do is that possible?!how to do it?
$row1 = mysqli_query($Database,"SELECT * FROM Table1 ORDER BY ID");
$row2 = mysqli_query($Database,"SELECT * FROM Table2 ORDER BY ID");
// Others Rows code
for ($num = 1; $num <= 6; $num++)
{
$RowNumber = "row" . $num; // Save RowNumber for use in while
echo '<tr>';
// when it's used it not work becuse it's just a string only and
// i want convert it to variable to use in while
while($row = mysqli_fetch_array($RowNumber))
{
echo '<td>
<div class="Image_DIV" id="'; echo $row['DIV_ID']; echo '">
<table>
<tr><td class="Image"><img class="ImageOfDiv"
src="3DGallery/Chosen/Small/'; echo $row['src']; echo '"/></td></tr>
<tr>
<td class="ImageDescribe">'; echo $row['describe']; echo '</td></tr></table>
</div>
</td>';
}
echo '</tr><tr>';
}
Try ${$RowNumber} instead of $RowNumber for your purpose, If you need more info you can find it here. But as Mr. raina77ow has commented arrays are a good option.
If Table1 and Table2 have the same columns then you should UNION them together in your SQL then work on a single results set.
$results = mysqli_query($Database, "SELECT ID, DIV_ID, src, describe FROM `Table1` UNION SELECT ID, DIV_ID, src, describe FROM `Table2` ORDER BY ID");
echo '<tr>';
$x = 0;
while($row = mysqli_fetch_array($results))
{
echo '<td>';
echo $row['DIV_ID'];
echo $row['src'];
echo $row['describe'];
echo '</td>';
if( ++$x % 3 == 0 ) {
echo '</tr><tr>';
}
}
echo '</tr>';
Try this:
$rows[] = mysqli_query($Database,"SELECT * FROM Table1 ORDER BY ID");
$rows[] = mysqli_query($Database,"SELECT * FROM Table2 ORDER BY ID");
// Others Rows code
foreach ($rows as $current)
{
echo '<tr>';
// loop the query
while($row = mysqli_fetch_array($current))
{
echo '<td>
<div class="Image_DIV" id="'; echo $row['DIV_ID']; echo '">
<table>
<tr><td class="Image"><img class="ImageOfDiv"
src="3DGallery/Chosen/Small/'; echo $row['src']; echo '"/></td></tr>
<tr>
<td class="ImageDescribe">'; echo $row['describe']; echo '</td></tr></table>
</div>
</td>';
}
echo '</tr><tr>';
}
You cant assign variables like that without using php Eval: http://www.php.net/eval, but that is realy bad practise!
or using ${$var} syntax. But arrays is probably easier to work with.
I'm trying to determine the right syntax from what I'm trying to do with MySQL. I'm basically saying that if a value in a certain column of a row of a table is equal to some session variables, I want to echo out info.
I have a table with subject, description and user. User is set by taking the current user's first name and last name and inserting it into the table under user. This is done by the following code:
$sql="INSERT INTO tbl_name (subject, description, user)
VALUES
('$_POST[subject]','$_POST[description]','$_SESSION[firstname] $_SESSION[lastname]')";
Then once I'm calling this data back out, I want to basically allow the user to delete content that they submitted themselves. The first step for me is to be able to display it in the table. I believe this is just syntax error, but I've confused myself now with how things are set up:
$sql = "SELECT * FROM tbl_name ORDER BY subject, description
LIMIT {$startpoint},{$limit}";
$result = $mysqli->query($sql);
$num_rows = mysqli_num_rows($result);
if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME EXAMPLES:</h1>";
echo "<table border='0'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->subject}</td>";
echo "<td>{$field->description}</td>";
echo "<td>{$field->user}</td>";
if('$field->user' == '$_SESSION[firstname] $_SESSION[lastname]'){
echo '<td>You can delete this</td>';
}
}
I figured the $field->user would equal $_SESSION[firstname] $_SESSION[lastname] because that's how it was initially submitted to the table (without the '.' for concatenation).
Any help would be appreciated. Thanks!
EDIT
Here's the result of my table output code. The results are actually being display with $cell instead of from within the for loop I believe. I've added the if statement in after the but it doesn't seem to recognize echo "<td>".$field->user."</td>"; which makes me think that that is where the problem lies. What I would like to do is be able to add the if statement in a immediately after `echo {$field->user}"; to keep the code clean. I think I've confused myself thoroughly:
if($num_rows>0){
$field_num = $mysqli->field_count;
echo "<h1>HERE ARE SOME JOBS:</h1>";
echo "<table border='0'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->subject}</td>";
echo "<td>{$field->description}</td>";
echo "<td>{$field->user}</td>";
if($field->user == $_SESSION[firstname]." ".$_SESSION[lastname]){
echo '<td>You can delete this</td>';
}
else{
echo "<td>".$field->user."</td>";
echo "<td>".$_SESSION[firstname]." ".$_SESSION[lastname]."</td>";
}
}
echo "</tr>\n";
while($row = mysqli_fetch_row($result))
{
echo"<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysqli_free_result($result);
}
else{
echo 'There are no jobs!';
}
I re-wrote the code in a way that was a little bit easier for me to understand (though maybe not the shortest way to do it):
while($row = mysqli_fetch_array($result))
{
echo"<tr>";
echo"<td>".$row['subject']."</td>";
echo"<td>".$row['description']."</td>";
echo"<td>".$row['user']."</td>";
if($row['user'] == $_SESSION['firstname']." ".$_SESSION['lastname']){
echo"<td>You can delete this</td>";
}
else{
echo"<td>Code didn't work</td>";
}
echo "</tr>\n";
}
It ended up working this way. If there's way to do this shorter then feel free to post it here otherwise thanks for the help!
I am sorry about the title as I am not sure whether my problem is in the PHP or the MSSQL query. I am pretty sure it is php.
I am repairing a PHP page that accesses two different MSSQL tables. the page worked fine until we had a system-wide change that created new department ID's and hence some new department names.
I have this almost completely fixed now except one minor thing. When you click List all departments, it Lists all the departments and each link has the correct individuals under them.
The problem is it lists a link for EACH individual in that department name for example if there are 10 people in the Business department we have ten links to Business and all have the same information under them.
I want to make it so there is only 1 link for each department. the two tables are directory (columns involved are Displayname and Department) and departments (columns involved are name and id)
Can someone please tell me where I need to change this so it only prints one link to each department? Is the problem with my SQL query or the PHP?
here is the code
function listDepts() {
$query = "SELECT directory.Lastname, directory.Firstname, directory.email,
directory.phone, directory.Office, directory.Department, departments.id,
departments.name FROM directory FULL JOIN departments ON
directory.Department=departments.id ORDER BY name";
$result = mssql_query($query);
echo "<h3>Please select a department:</h3>\n";
echo "<ul>\n";
for ($i=0; $i<mssql_num_rows($result); ) {
$info = mssql_fetch_assoc($result);
echo "<li>$info[name]</li>\n";
}
echo "</ul>\n\n";
}
Other query
function displayResults($query) {
$result = mssql_query($query);
if (mssql_num_rows($result) > 0) {
for ($i=0; $i<mssql_num_rows($result); $i++) {
$info = mssql_fetch_assoc($result);
if ($info[dept_name] != $last_dept) {
if ($i > 0) {
echo "</table>\n\n";
}
echo "<h3>$info[dept_name]</h3>\n\n";
echo "<table id=\"directory_table\">\n";
echo "<tr>\n";
echo "<th>Name</th>\n";
echo "<th>E-mail</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Office</th>\n";
echo "<th>Title</th>\n";
echo "</tr>\n";
}
if (!$info[dept_name] && $i==0) {
echo "<table id=\"directory_table\">\n";
echo "<tr>\n";
echo "<th>Name</th>\n";
echo "<th>E-mail</th>\n";
echo "<th>Phone</th>\n";
echo "<th>Office</th>\n";
echo "<th>Title</th>\n";
echo "</tr>\n";
}
if ($i % 2 == 0) {
echo "<tr class=\"even\">\n";
} else {
echo "<tr class=\"odd\">\n";
}
echo "<td>";
echo ($info[Firstname]) ? "$info[Firstname]" . " " . "$info[Lastname]" : " ";
echo "</td>\n";
echo "<td>";
echo ($info[email]) ? "$info[email]" : " ";
echo "</td>\n";
echo "<td>";
echo ($info[phone]) ? "$info[phone]" : " ";
echo "</td>\n";
echo "<td>";
echo ($info[office]) ? "$info[office]" : " ";
echo "</td>\n";
echo "<td>";
echo ($info[title]) ? "$info[title]" : " ";
echo "</td>\n";
$last_dept = $info[dept_name];
}
echo "</table>\n\n";
} else {
echo "<p>No results found.</p>\n\n";
}
}
If you're only using these two fields:
echo "<li>$info[name]</li>\n";
Why not just make your select query:
SELECT DISTINCT
name
FROM
departments
ORDER BY
name
You're not using anything from the directory table, so you don't need to take anything from it.
If you've got multiple departments, try:
echo "<li>$info[name]</li>\n";
And then updating your department code to search for employees in the department with that name.
Department code:
$theSQL = "SELECT * FROM directory WHERE department IN (SELECT id FROM departments WHERE name='$department') ORDER BY Lastname");
In your for loop at the end of each iteration $i needs to be evaluated.
It should be like
for ($i=0; $i<mssql_num_rows($result); $i++)
Another way (generally) of looping is
$query = mssql_query('your_query');
while ($row = mssql_fetch_assoc($query)){
echo "<li>$row['name']</li>\n";
}