Sorry for little weird tittle, english is not my native language, and its little hard for me to express myself about this, so I hope i'll get clearer in explanation :)
I have this piece of code, to fetch all rows from MYSQL db, and when it fetch it out, i want to do some action on everyone of that row. (I want to display different PDF files stored in DB).
Take a look
include_once 'dbcon.php';
$query = mysql_query(" SELECT email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='' value= 'button' > <br />";
}
How to increment title of button name, on every row, so I can use it later to access it via POST method to display PDF?
I want something like this...
1st row : name='button1'
2nd row : name ='button2' etc etc..
When im outputing HTML with PHP it only allow me to read some variables, not some functions stored in quotation marks.
Please help guys! Thanks
You can end the string and concatenate it with an expression. In this case, I use $i++ which works like a counter. It returns the value of $i and then increments $i for the next iteration:
$i = 1;
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='button" . $i++ . "' value= 'button' > <br />";
}
But 'row 1' doesn't always have to be the same row. Due to rows being added and removed you may get a different result, so 'button1' isn't a very good name. It would be better if you could use the name or id of the row. So if the person table has a numeric id as well, you could use that:
$query = mysql_query(" SELECT id, email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='button{$row['id']}' value= 'button' > <br />";
}
As you can see here, I didn't concat, but instead embedded the array variable in the string. This works similar to normal variables, except you have to put braces around the variable. Of course, you could still concatenate the value as in the first example.
We use [] to create an array of the buttons
I'm not in a live environment so I can't test it.
Accessing via $_POST:
$size = sizeof($_POST['button']);
for($i = 0; $i < $size; $i++){echo $_POST['button'][$i];}
// Code
include_once 'dbcon.php';
$query = mysql_query(" SELECT email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . "<input type='button' name='button[]' value= 'button' > <br />";
}
$query = mysql_query(" SELECT id, email, name FROM upload_jezici ");
while ($row = mysql_fetch_array($query)) {
echo $row['email']. " " . $row['name'] . " ";
}
Yes, i've tried to increment with for loop, but no luck.. This is doing a job very well :)
Good idea. Thanks guys very much!
Related
I have installed wordpress and opencart in same database. Trying to get wordpress posts table inside opencart module. got the mysql query to fetch all information except image. I dont why images are different from the post in loop of result. Kindly guide, following is the code.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tablename";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);}
$sql = "SELECT * FROM wp_posts WHERE post_type = 'attachment' ORDER BY ID DESC LIMIT 3";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<div class="col-sm-4">';
echo "Post ID: " . $row["ID"] . " / ";
echo "Post Title: " . $row["post_title"] . " / ";
echo "Post Title: " . $row["post_date"] . " / ";
echo '<img src="' . $row['guid'] . '" class="img-responsive">';
echo '</div>';
}
} else {
echo "0 results";
}
Run your query in phpMyAdmin and check if result you got is the same as you want (you get those pictures that you want).
Then I advise you to set the checkpoint inside the loop and look at the data. Debugging is a very powerful thing in finding errors, it's time to start using it
I would like to use PDO for selecting (searching) a database.
The search 'form' has MULTIPLE fields that can be used.. 1 or many can be filled in to help refine the search. (or there can be many o them left blank/empty)
here is what I have been using (locally):
//localhost details
$db_username="root"; //database user name
$db_password="";//database password
$db_database="test"; //database name
$db_host="localhost";
mysql_connect($db_host,$db_username,$db_password);
#mysql_select_db($db_database) or die("Unable to connect to database.");
if(isset($_POST['submit'])) {
// define the list of fields
$fields = array('first', 'trialdate', 'wcity', 'wstate', 'plantif');
$conditions = array();
//loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && $_POST[$field] != '') {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$conditions[] = "`$field` LIKE '%" . mysql_real_escape_string($_POST[$field]) . "%'";
}
}
//build the query
$query = "SELECT * FROM myTable ";
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= "WHERE " . implode (' OR ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}
$result = mysql_query($query);
if(isset($_POST['submit'])) {
while($row = mysql_fetch_array($result)) {
echo $row['first'] . "<br />"; //individual value
//build panels that displays everything from row..etc
}
}
}
this has been working fine... but I'd like convert to using the PDO approach.
I gave it a few tries...but am missing something here..
heres what I've tried so far..
//localhost details
$db_username="root"; //database user name
$db_password="";//database password
$db_database="test"; //database name
$db_host="localhost";
//PDO DB connection
$conn = new PDO('mysql:host='.$db_host.'dbname='.$db_database.'charset=utf8', $db_username, $db_password);
if(isset($_POST['submit'])) {
$stmt = $conn->prepare('SELECT * FROM myTable WHERE first LIKE :first OR trialdate LIKE :trialdate OR wcity LIKE :wcity OR wstate LIKE :wstate OR plantif LIKE :plantif');
//build query placeholders (*note: use bindValue for $_POST values)
$stmt->bindValue(':first', '%' . $_POST['first'] . '%');
$stmt->bindValue(':trialdate', '%' . $_POST['trialdate'] . '%');
$stmt->bindValue(':wcity', '%' . $_POST['wcity'] . '%');
$stmt->bindValue(':wstate', '%' . $_POST['wstate'] . '%');
$stmt->bindValue(':plantif', '%' . $_POST['plantif'] . '%');
$stmt->execute();
foreach ($stmt as $row) {
// do something with $row
echo $row['first'] . "<br />"; //individual value
}
}
I could use help on getting the PDO example working with a displayed result/row/value?
I'm having a bit of trouble paginating my sql query results using PHP. I have tried a few different solutions but have had no success so far. Would someone be able to help me please? Below is my current code I use to display all results however I'd like them splitting into pages of 50.
<?
//query
$data = mysql_query("SELECT * FROM names");
//counts_result
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "ERROR";
}
else {
echo "<div class='search_result_output'>
SEARCH SUCCESS
</div><hr>";
}
?>
<?
//display results
while($result = mysql_fetch_array( $data ))
{
echo "<div class='panel panel-default'>";
echo "<div class='panel-heading'>";
echo "<h4 class='panel-title'>";
echo $result['name'];
echo "</h4>";
echo "</div>";
echo "<div class='game_actions' style='float:right; margin-top:-40px; margin-right:10px;'>";
echo "<a data-toggle='collapse' class='btn btn-custom' data-parent='#accordion' href='#".$result['id']."' title='More Info on ".$result['name']."'><i class='fa fa-plus'></i></a>";
echo "</div>";
echo "<div id='".$result['id']."' class='panel-collapse collapse'>";
echo "<div class='panel-body'>";
echo $result['name'];;
echo "</div>";
echo "</div>";
echo "</div>";
}
}
?>
You have this:
$data = mysql_query("SELECT * FROM names");
instead use this:
$page = (isset($_GET["page"])) ? $_GET["page"] : "0";
$data = mysql_query("SELECT * FROM names limit ".$page.", 50");
Display your links accordingly; add the page value to them. Your paging depends on the get parameter named "page".
EDIT:
If you want to display a link pointing to the 5th page, for instance, then:
echo '5';
4 * 50 = page index * page size. Page index is 4, as on the first page you have no offset. It is difficult to add any more help, since I do not know what kind of paging you need. However, this edit should help you to start solving your problem.
try something like this query and then just just change parameter i.e (3 and 8)
Select * from
(select Id,Name ,ROW_NUMBER() over (order by Id) as RowNum from Person.dbo.pers
) tbl
Where tbl.RowNum between 3 and 8
it gets 3 to 8 rows
I have this code which i use in order to make appear all the names of a table of a database.
It used to work... but suddenly it won't make appear anything..
Could you please take a look?
I'm working with SQL.
$section = "SELECT * FROM forma";
$res = odbc_exec($connection, $section) or die(odbc_error());
$firstrow = false;
while ($row = odbc_fetch_array($res)){
if (!$firstrow) {
foreach ($row as $column => $value) {
echo "<label> " . $column . "</label>";
echo "<input type='checkbox' name='data[]' value='" . $column . "' /><br/><br/>";
}
$firstrow = true;
}
}
Thanks
This is a fairly nasty way of retrieving the column names for a table. What it is doing is reading all the data in the table, ignoring the result set and only using it for the column names. What is happening however is that the table is empty and so nothing is being returned at all.
You need to amend the query to look at the meta data rather than the table itself. You will need to rework it. This SQL will retrieve the column names for that table for you...
Select Columns.Name
From Sys.Columns
Where Object_Name(Columns.Object_id) = 'forma'
Order By Columns.Column_Id;
After that you will need to rejig your code to take advantage of it.
I use codeigniter, and I need to get data from 2 different table. for now it returns data only from works_image table. how can I get data from both 2 table ?
thanks a lot!!
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "";
}
As long as you're doing a SELECT * (Why is this a bad idea?), you shouldn't need to specify tables with the call to select(). It will select all fields by default.
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
Should work fine.
Instead, what you really should be doing, is specifying exactly which fields you need:
$this->db->select('works_image.id, works_image.name, works_image.id_work, works.id, works.name'); // (or whichever fields you're interested in)
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
This way you can be sure that (a) you're not pulling unnecessary data from your DB, and (b) your code won't break if/when you modify your DB schema.
This post should answer your question: http://www.whypad.com/posts/codeigniter-activerecord-join-tip/178/
In short, you need to rewrite your select statement from
$this->db->select('works_image.*', 'works.*');
to this:
$this->db->select('works_image.*, works.*');
Note that this was the first result on Google for 'CodeIgniter join'. Try Googling your questions first. You can often get yourself a faster answer :)
You should be to write below code
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "
";
}
I think that you get your result