I'm making a table with translation:
English | Italian | French
help | ayuto | amo
and am creating a functionality to add new language (Russian). I have it all set up but the import doesn't work correctly. It imports the values under already existing fields. So it doesn't add them directly under the newly added language. Looks like this:
English | Italian | French | Russian(added)
help | ayuto | amo |
| | | помощь(imported)
| | | извините(imported)
I need that to start it from the top like this:
English | Italian | French | Russian(added)
help | ayuto | amo | помощь(imported)
| | | извините(imported)
Here is the code:
if (isset($_POST['submit'])){
$lang_name = htmlspecialchars($_POST['lang_name'], ENT_QUOTES);
$str = strtolower($lang_name);
$lang_lawname = str_replace(' ', '_',$str);
$sql = "INSERT INTO translation_lang (languages) VALUES ('".$lang_name."')";
$sql2 = "ALTER TABLE translation ADD $lang_lawname VARCHAR( 255 ) DEFAULT ''";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql2)) {
die('Error: ' . mysqli_error($con));
}
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
//echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
//echo "<h2>Displaying contents:</h2>";
readfile($_FILES['filename']['tmp_name']);
//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
do {
if ($data[0]) {
$import="INSERT into translation($lang_lawname) values('$data[0]')";
mysqli_query($con,$import) or die(mysqli_error());
}
}
while ($data = fgetcsv($handle,1000,",","'"));
}
Related
I want to export my code to excel however the header is also dynamic. Here is my code, i need assistance in exporting my data. This code below shows a dynamic table i showed in user view and needs to be exported. The view is:
"title: course/section"
"header: student id | student name | program | term | dynamic date 1 | Remarks (remarks why u are absent) including date 1 | date 2 | Remarks (remarks why u are absent) including date 2 | and so on.."
"table data: 20122222 |pinky webb |computer science | 3 | Absent | With medical certificate | present | no remark | present | no remark | late | no remark | and do on... "
"table data 2: 20122333 |Mane Sharpay|computer science | 3 | Absent | With medical certificate | present | no remark | Late | no remark | late | no remark | and so on... "
and so on...
basically, it shows the student and its attendance per date horizontally with a dynamic header of dates. sorry if im noob hehe but ill give a thumbs up i promise for ur effort
<?php
$query = "SELECT course_id, sections_id from current_att_view inner join professor_tbl on current_att_view.professor_id = professor_tbl.professor_id where professor_live_account = '".$_SESSION['username']."' group by course_id, sections_id";
$result1 = mysqli_query($link, $query);
while ($col1 = mysqli_fetch_array($result1))
{
$reslt;
echo '<h3 class="course-sect">'.$col1[0].'/'.$col1[1].'</h3>';
$qry = "Call Get_Attendance_Course_Section('".$_SESSION['username']."','".$col1[0]."','".$col1[1]."')";
$reslt = mysqli_query($link, $qry);
echo '<table class="table table-bordered">';
echo '<tr>';
if (!$reslt) {
printf("Error: %s\n", mysqli_error($link));
exit();
}
else{
while ($fieldinfo = mysqli_fetch_field($reslt)) {
if($fieldinfo->name != "Course" && $fieldinfo->name != "Section" && $fieldinfo->name != "Course Name" && $fieldinfo->name != "Schedule")
{
echo '<th>';
echo $fieldinfo->name;
echo '</th>';
}
}
echo '</tr>';
while ($rows = mysqli_fetch_array($reslt))
{
for ($i=0; $i < (count($rows)/2); $i++) {
if($i != 3 && $i != 4 && $i != 5 && $i != 6){
echo '<td>';
echo $rows[$i];
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
mysqli_next_result($link);
}
}
// $reslt =mysqli_query($link, $qry);
?>
<input type=hidden name=date value='<?php echo date("F d, Y",strtotime($date));?>'>
<input type="hidden" name="outy" value="<?php echo $sql; ?>">
<input type="submit" name="export" value="Export" class="submit" />
Im new to PHP MySQL. Im developing a songbook site.
I want to create tables alphabetically in separate pages for each alphabet.
When a user click an alphabet in the menu it will direct to this page site/publicsearch.php?browse=a
This is the database :
| ID | TITLE | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
| 1 | Amazing love | XXXXXX | Love | a |
| 2 | Above all | XXXXXX | Worship | a |
| 3 | BXXXX | XXXXXX | Love | b |
| 4 | BXXXX | XXXXXX | Worship | b |
I pull the above database table like this : It works fine.
<?php
$servername = "localhost";
$username = "xxxxxxx";
$password = "xxxxxxxx";
$db_name = "xxxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Connected successfully";
exit();
}
// Attempt select query execution
$sql = "SELECT * FROM lyrics_a";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table'>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>Title</th>";
echo "<th>artist</th>";
echo "<th>cateogry</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td> " . $row['eng_title'] . " " . $row['tel_title'] . " </td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
I need separate alphabetical tables like this :
Alphabet "a" table results `site/publicsearch.php?browse=a`
| ID | TITLE | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
| 1 | Amazing love | XXXXXX | Love | a |
| 2 | Above all | XXXXXX | Worship | a |
Alphabet "b" table results `site/publicsearch.php?browse=b`
| ID | TITLE | ARTIST | CATEGORY | ALPHABET |
+----------------------------------------------------+
| 3 | BXXXX | XXXXXX | Love | b |
| 4 | BXXXX | XXXXXX | Worship | b |
I tried the below code doesn't work.
<?php
$servername = "localhost";
$username = "xxxxxx";
$password = "xxxxxxx";
$db_name = "xxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$alphabet = $_GET['alphabet'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`='" . $alphabet . "'";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result)) {
echo "<br><br>";
echo $row['id'];
echo $row['title'];
echo $row['lyrics'];
echo $row['alphabet'];
}
?>
I'm newbie. Please help. Thank you.
$alphabet = $_GET['alphabet'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`='" . $alphabet . "'";
replace with:
$alphabet = mysqli_real_escape_string($conn,$_GET['browse']);
$query = "SELECT * FROM `lyrics` WHERE `alphabet`= '" . $alphabet . "' ";
You propose the following URL: publicsearch.php?browse=a
But in your script you are using the request parameter $_GET['alphabet']
In addition, you don't need a separate column alphabet in your database, you can use SQL's LIKE:
$letter = $_GET['browse'];
$sql = "SELECT * FROM `lyrics` WHERE title LIKE '{$letter}%'"
Having a query that draws multiple lines from a DB, each with an ID, a path and another ID. Is it possible to pass the path by the filesize() function and mark if two or more lines have the same file size?
Here's my code:
$q2 = "SELECT idMultimedia, pathURLMult, Type_idType FROM Multimedia";
$e2 = mysqli_query( $conn, $q2 );
while( $r2 = mysqli_fetch_array($e2) ){
list($width, $height) = getimagesize($r2["pathURLMult"]);
$size = filesize($r2["pathURLMult"]);
echo $r2["idMultimedia"] . " | " . $r2["pathURLMult"] . " | " . $width. " | " . $height. " | " . $size . "<br>";
}
Example of a possible Database output:
1 | randomguy1.jpg | 2
2 | A-GUY.jpg | 3
3 | lol.jpg | 2
4 | randomguy2.jpg | 1
randomguy1.jpg & randomguy2.jpg have the same file size... I need to show that for both pics! How can I do this?
Any help? Thanks in advance!
All i want to do is to store the user entered values to a database.There is a table and it has many text boxes where the user enters his input.Upon clicking submit button the values should be stored in the mysql table.
I'm getting some errors right now.I'm completely new to php,Below I've attached the code and screenshots.
//workingcode.php
<?php
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database
mysql_select_db("sms", $con);
$result = mysql_query("SELECT * FROM dept_info", $con) or die(mysql_error());
echo "<table border='1'>";
echo '<tr> <th>Sl No</th> <th>USN</th> <th>Name</th><th>code1</th><th>code 2</th> <th>code 3</th> <th>code 4</th> <th>code 5</th>
<th>code 6</th> <th>code 7</th> <th>code 8</th></tr>';
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['usn'];
echo "</td><td>";
echo $row['name'];
echo "</td>";
for ($i=1; $i <= 8; $i++) {
echo "<td> <input type='text' name='sl[{$row['usn']}][$i]' size='2' /> </td>" ;
}
echo '</tr>';
}
echo '
<form action = "insert.php" method = "post">
<div align="bottom">
<input type = "submit" align = "BOTTOM" value = "Proceed">
</div>
</form>
';
?>
I've linked this to an insert.php file as shown below..
<html>
<head>
<title>Marks Entry Results</title>
</head>
<body>
<h1>Student Marks Entry Results</h1>
<?php
$insertData = array();
foreach ($_POST['sl'] as $usn => $codes) {
foreach ($codes as $sl) {
$insertData[] = sprintf("('%s', %d)", mysqli_real_escape_string($usn), intval($sl));
}
}
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database
mysql_select_db("sms", $con);
$query = "INSERT INTO mytable (usn, code1) VALUES\n" . join(",\n", $insertData);
$result = mysql_query($query);
if ($result) {
echo mysql_affected_rows()." Marks inserted into database.";
} else {
echo "An error has occurred. Not added.";
}
mysql_close();
?>
</body>
</html>
//The description of the table which i made for this is
/*mysql> desc mytable;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| usn | varchar(50) | NO | PRI | NULL | |
| code1 | int(2) | YES | | NULL | |
| code2 | int(2) | YES | | NULL | |
| code3 | int(2) | YES | | NULL | |
| code4 | int(2) | YES | | NULL | |
| code5 | int(2) | YES | | NULL | |
| code6 | int(2) | YES | | NULL | |
| code7 | int(2) | YES | | NULL | |
| code8 | int(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
9 rows in set (0.01 sec)
*/
The textboxes are not inside any form tag so when you hit submit the value of textboxes doesn't get post back to server...Try following.
echo '<form action = "insert.php" method = "post">';
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['usn'];
echo "</td><td>";
echo $row['name'];
echo "</td>";
for ($i=1; $i <= 8; $i++) {
echo "<td> <input type='text' name='sl[{$row['usn']}][$i]' size='2' /> </td>" ;
}
echo '</tr>';
}
echo '
<div align="bottom">
<input type = "submit" align = "BOTTOM" value = "Proceed">
</div>
</form>';
//insert.php
<?php
$insertData = array();
foreach ($_POST['sl'] as $usn => $codes) {
foreach ($codes as $sl) {
$insertData[] = sprintf("('%s', %d)", mysqli_real_escape_string($usn), intval($sl));
}
}
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database
mysql_select_db("sms", $con);
$query = "INSERT INTO mytable (usn, code1) VALUES\n" . join(",\n", $insertData);
$result = mysql_query($query);
if ($result) {
echo mysql_affected_rows()." Marks inserted into database.";
} else {
echo "An error has occurred. Not added.";
}
mysql_close();
?>
Basic question!
I have 2 tables
PRODUCE
+-----+--------------+
| id | fruit_name |
+--------------------+
| 1 | Apple |
| 2 | Banana |
| 3 | Carrot |
+-----+--------------+
VARIETIES
+-----+---------------+----------------+
| id | fk_fruit_id | variety_name |
+-----+---------------+----------------+
| 1 | 1 | Cox |
| 2 | 1 | Braeburn |
| 3 | 2 | Chester |
| 4 | 3 | Kotaka |
| 5 | 3 | Imperial |
| 6 | 3 | Oneal |
+-----+---------------+----------------+
I'd like to output a list of varieties per fruit e.g.
APPLE - Cox, Braeburn
BANANA - Chester
CARROT - Kotaka, Imperial, Oneal
My current code is
$query = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id";
$result = mysql_query($query) or die('Error : ' . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$produce_fruit_code = $row['fruit_code'];
$variety_name = $row['variety_name'];
echo $produce_fruit_code.' - '.$variety_name.'<br/>';
}
which outputs:
Apple - Cox
Apple - Braeburn
Banana - Chester
Carrot - Kotaka
Carrot - Imperial
Carrot - Oneal
Not a million miles away, but still not there. Any help is much appreciated, thanks!
You probably could get one chunky sql statement to do that for you but I'd opt for data juggling with arrays.
For example (not tested and excuse the formatting):
$query = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$res=array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$produce_fruit_code = $row['fruit_code'];
$variety_name = $row['variety_name'];
if(isset($res[$produce_fruit_code])){
$res[$produce_fruit_code]+=','.$variety_name;
}else{
$res[$produce_fruit_code]=$variety_name;
}
}
print_r($res);
If you are using MySQL you can use the group_concat extension on grouping. Something along the lines of:
SELECT
f.fruitname as fruit,
GROUP_CONCAT(distinct v.varietyname separator ',') as variety
FROM fruit f JOIN varieties v ON produce.id = varieties.fk_fruit_id;
or similar. Sorry my sql is a little rusty quite now.
For more look at this article
http://www.mysqlperformanceblog.com/2006/09/04/group_concat-useful-group-by-extension/
and of course here:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
If you dont work with MySQL and your database doesn't support group_concat think about buffering those results. In large databases and with many simultaneous users your application can considerably slow down when having to download all data and store it in locally every time.
This won't get you all the way, but it will get you most of what you want. There are some edge cases that are problematic.
$query = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$produce_fruit_code = "";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($produce_fruit_code != $row['fruit_code'])
{
$produce_fruit_code = $row['fruit_code'];
echo "<br/>".$produce_fruit_code." - ". $row['variety_name'];
} else {
echo ", ".$row['variety_name'];
}
}
$query = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id";
echo "<dl>";
$result = mysql_query($query) or die('Error : ' . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if($row['fruit_name'] != $current_fruit_name) {
$current_fruit_name = $row['fruit_name'];
echo "<dt>$current_fruit_name</dt>";
}
echo "<dd>" . $row['variety_name'] . "</dd>";
}
echo "";
If you want some CSS that will make a definition list look like the Name - X,Y,Z like in the question, let me know.
You can query this directly
SELECT
f.fruitname as fruit,
GROUP_CONCAT(distinct v.varietyname separator ',') as variety
FROM fruit f JOIN varieties v ON produce.id = varieties.fk_fruit_id;
GROUP BY produce.id
Sort everything into arrays in your while loop. This should work:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$produce_fruit_code = $row['fruit_code'];
$variety_name = $row['variety_name'];
if ($produce_fruit_code == "Apple") {
$apple_array[] = $variety_name;
}
if ($produce_fruit_code == "Banana") {
$banana_array[] = $variety_name;
}
if ($produce_fruit_code == "Carrot") {
$carrot_array[] = $variety_name;
}
}
echo "Apples:" . implode(", ", $apple_array) . "<br/>";
echo "Bananas:" . implode(", ", $bananas_array) . "<br/>";
echo "Carrots:" . implode(", ", $carrots_array) . "<br/>";