38703-vm/db/seed_cars.php
Flatlogic Bot 8d996da0d9 sad
2026-02-23 09:05:29 +00:00

78 lines
3.5 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
try {
$pdo = db();
// Get admin user id
$adminId = $pdo->query("SELECT id FROM users WHERE role = 'admin' LIMIT 1")->fetchColumn();
if (!$adminId) {
die("Admin user not found. Please run setup.php first.");
}
// Clear existing cars to avoid duplicates during seeding if needed,
// but better to just check count or add specifically.
$stmt = $pdo->query("SELECT COUNT(*) FROM cars");
if ($stmt->fetchColumn() > 5) {
echo "Cars already seeded.";
exit;
}
$afghanCities = ['Kabul', 'Herat', 'Mazar-i-Sharif', 'Kandahar', 'Jalalabad', 'Kunduz', 'Ghazni', 'Balkh'];
$brands = [
'Toyota' => ['Corolla', 'Camry', 'Land Cruiser', 'Hilux', 'Prado', '4Runner'],
'Mercedes-Benz' => ['G-Wagon', 'S-Class', 'E-Class', 'C-Class', 'GLE'],
'Lexus' => ['LX570', 'RX350', 'GX460', 'ES350'],
'Hyundai' => ['Elantra', 'Tucson', 'Santa Fe', 'Accent'],
'Honda' => ['Civic', 'CR-V', 'Accord'],
'Ford' => ['F-150', 'Mustang', 'Explorer'],
'BMW' => ['X5', 'X6', '5 Series', '7 Series']
];
$descriptions = [
"Excellent condition, very well maintained.",
"Full option, armored, and ready for any terrain.",
"Fuel efficient, perfect for city driving.",
"Luxury interior, premium sound system, and smooth ride.",
"Recently imported, custom cleared, and plate registered.",
"Powerful engine, off-road capabilities, and spacious.",
"Very clean inside and out, low mileage.",
"Top of the line model with all modern features."
];
$insertCar = $pdo->prepare("INSERT INTO cars (user_id, brand, model, year, price, city, description, status, is_hot_deal) VALUES (?, ?, ?, ?, ?, ?, ?, 'approved', ?)");
$insertImage = $pdo->prepare("INSERT INTO car_images (car_id, image_path, is_main) VALUES (?, ?, 1)");
$carImages = [
'https://images.pexels.com/photos/170811/pexels-photo-170811.jpeg?auto=compress&cs=tinysrgb&w=600',
'https://images.pexels.com/photos/112460/pexels-photo-112460.jpeg?auto=compress&cs=tinysrgb&w=600',
'https://images.pexels.com/photos/3729464/pexels-photo-3729464.jpeg?auto=compress&cs=tinysrgb&w=600',
'https://images.pexels.com/photos/912413/pexels-photo-912413.jpeg?auto=compress&cs=tinysrgb&w=600',
'https://images.pexels.com/photos/116675/pexels-photo-116675.jpeg?auto=compress&cs=tinysrgb&w=600',
'https://images.pexels.com/photos/210019/pexels-photo-210019.jpeg?auto=compress&cs=tinysrgb&w=600',
'https://images.pexels.com/photos/337909/pexels-photo-337909.jpeg?auto=compress&cs=tinysrgb&w=600',
'https://images.pexels.com/photos/1149137/pexels-photo-1149137.jpeg?auto=compress&cs=tinysrgb&w=600'
];
for ($i = 0; $i < 20; $i++) {
$brand = array_rand($brands);
$model = $brands[$brand][array_rand($brands[$brand])];
$year = rand(2015, 2024);
$price = rand(5000, 150000);
$city = $afghanCities[array_rand($afghanCities)];
$desc = $descriptions[array_rand($descriptions)];
$isHot = (rand(1, 10) > 7) ? 1 : 0;
$insertCar->execute([$adminId, $brand, $model, $year, $price, $city, $desc, $isHot]);
$carId = $pdo->lastInsertId();
$imageUrl = $carImages[array_rand($carImages)];
$insertImage->execute([$carId, $imageUrl]);
}
echo "Successfully seeded 20 cars.";
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}